-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
159 lines (135 loc) · 3.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"os"
"rare/cmd"
"rare/cmd/helpers"
"rare/pkg/color"
"rare/pkg/expressions/funcfile"
"rare/pkg/expressions/funclib"
"rare/pkg/expressions/stdlib"
"rare/pkg/fastregex"
"rare/pkg/humanize"
"rare/pkg/logger"
"rare/pkg/multiterm"
"rare/pkg/multiterm/termunicode"
"github.com/urfave/cli/v2"
)
type appModifier func(app *cli.App)
var appModifiers []appModifier
func buildApp() *cli.App {
app := cli.NewApp()
app.Usage = "A fast regex parser, extractor and realtime aggregator"
app.Version = fmt.Sprintf("%s, %s; regex: %s", version, buildSha, fastregex.Version)
app.Description = `Aggregate and display information parsed from text files using
regex and a simple handlebars-like expressions.
Run "rare docs overview" or go to https://rare.zdyn.net for more information
https://github.com/zix99/rare`
app.Copyright = `rare Copyright (C) 2019 Chris LaPointe
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions`
app.UseShortOptionHandling = true
app.Suggest = true
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "nocolor",
Aliases: []string{"nc"},
Usage: "Disables color output",
},
&cli.BoolFlag{
Name: "noformat",
Aliases: []string{"nf"},
Usage: "Disable number formatting",
},
&cli.BoolFlag{
Name: "nounicode",
Aliases: []string{"nu"},
Usage: "Disable usage of unicode characters",
},
&cli.BoolFlag{
Name: "noload",
Aliases: []string{"nl"},
Usage: "Disable external file loading in expressions",
},
&cli.StringSliceFlag{
Name: "funcs",
EnvVars: []string{"RARE_FUNC_FILES"},
Usage: "Specify filenames to load expressions from",
},
&cli.BoolFlag{
Name: "color",
Usage: "Force-enable color output",
},
&cli.BoolFlag{
Name: "notrim",
Usage: "By default, rare will trim output text for in-place updates. Setting this flag will disable that",
},
}
// When showing default help, exit with an error code
app.Action = func(c *cli.Context) error {
var err error
args := c.Args()
if args.Present() {
err = cli.ShowCommandHelp(c, args.First())
} else {
err = cli.ShowAppHelp(c)
}
if err != nil {
return err
}
return cli.Exit("", helpers.ExitCodeInvalidUsage)
}
app.Commands = cmd.GetSupportedCommands()
app.Before = cli.BeforeFunc(func(c *cli.Context) error {
if c.Bool("nocolor") {
color.Enabled = false
} else if c.Bool("color") {
color.Enabled = true
}
if c.Bool("noformat") {
humanize.Enabled = false
}
if c.Bool("notrim") {
multiterm.AutoTrim = false
}
if c.Bool("nounicode") {
termunicode.UnicodeEnabled = false
}
if c.Bool("noload") {
stdlib.DisableLoad = true
}
if funcs := c.StringSlice("funcs"); len(funcs) > 0 {
cmplr := funclib.NewKeyBuilder()
for _, ff := range funcs {
funclib.TryAddFunctions(funcfile.LoadDefinitionsFile(cmplr, ff))
}
}
return nil
})
app.ExitErrHandler = func(c *cli.Context, err error) {
// Suppress built-in handler (Which will exit before running any After())
// Handle exit-codes in main()
// This also allows for better unit testing...
}
// Apply any plugin/modifiers
for _, modifier := range appModifiers {
modifier(app)
}
return app
}
func cliMain(args ...string) error {
return buildApp().Run(args)
}
func main() {
err := cliMain(os.Args...)
if err != nil {
if msg := err.Error(); msg != "" {
logger.Print(msg)
}
if v, ok := err.(cli.ExitCoder); ok {
os.Exit(v.ExitCode())
}
os.Exit(helpers.ExitCodeInvalidUsage)
}
}