This repository was archived by the owner on May 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
471 lines (385 loc) · 10.5 KB
/
cli.go
File metadata and controls
471 lines (385 loc) · 10.5 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package broccli
import (
"context"
"flag"
"fmt"
"io"
"os"
"path"
"reflect"
"sort"
"strings"
"text/tabwriter"
)
// Broccli is main CLI application definition.
// It has a name, description, author which are printed out to the screen in the usage syntax.
// Each CLI have commands (represented by Command). Optionally, it is possible to require environment
// variables.
type Broccli struct {
name string
usage string
author string
commands map[string]*Command
env map[string]*param
parsedFlags map[string]string
parsedArgs map[string]string
}
// NewBroccli returns pointer to a new Broccli instance. Name, usage and author are displayed on the syntax screen.
func NewBroccli(name, usage, author string) *Broccli {
cli := &Broccli{
name: name,
usage: usage,
author: author,
commands: map[string]*Command{},
env: map[string]*param{},
parsedFlags: map[string]string{},
parsedArgs: map[string]string{},
}
return cli
}
// Command returns pointer to a new command with specified name, usage and handler. Handler is a function that
// gets called when command is executed.
// Additionally, there is a set of options that can be passed as arguments. Search for commandOption for more info.
func (c *Broccli) Command(
name, usage string,
handler func(ctx context.Context, cli *Broccli) int,
opts ...CommandOption,
) *Command {
c.commands[name] = &Command{
name: name,
usage: usage,
flags: map[string]*param{},
args: map[string]*param{},
env: map[string]*param{},
handler: handler,
options: commandOptions{},
}
for _, opt := range opts {
opt(&(c.commands[name].options))
}
return c.commands[name]
}
// Env returns pointer to a new environment variable that is required to run every command.
// Method requires name, eg. MY_VAR, and usage.
func (c *Broccli) Env(name string, usage string) {
c.env[name] = ¶m{
name: name,
usage: usage,
flags: IsRequired,
options: paramOptions{},
}
}
// Flag returns value of flag.
func (c *Broccli) Flag(name string) string {
return c.parsedFlags[name]
}
// Arg returns value of arg.
func (c *Broccli) Arg(name string) string {
return c.parsedArgs[name]
}
// Run parses the arguments, validates them and executes command handler.
// In case of invalid arguments, error is printed to stderr and 1 is returned. Return value should be treated as exit
// code.
func (c *Broccli) Run(ctx context.Context) int {
// display help, first arg is binary filename
if len(os.Args) < 2 || os.Args[1] == "-h" || os.Args[1] == "--help" {
c.printHelp()
return 0
}
for _, commandName := range c.sortedCommands() {
if commandName != os.Args[1] {
continue
}
// display command help
if len(os.Args) > 2 && (os.Args[2] == "-h" || os.Args[2] == "--help") {
c.commands[commandName].printHelp()
return 0
}
// check required environment variables
if len(c.env) > 0 {
for env, param := range c.env {
envValue := os.Getenv(env)
param.flags |= IsRequired
err := param.validateValue(envValue)
if err != nil {
fmt.Fprintf(
os.Stderr,
"ERROR: %s %s: %s\n",
c.getParamTypeName(ParamEnvVar),
param.name,
err.Error(),
)
c.printHelp()
return 1
}
}
}
// parse and validate all the flags and args
exitCode := c.parseFlags(c.commands[commandName])
if exitCode > 0 {
return exitCode
}
return c.commands[commandName].handler(ctx, c)
}
// command not found
c.printInvalidCommand(os.Args[1])
return 1
}
func (c *Broccli) sortedCommands() []string {
commandNames := reflect.ValueOf(c.commands).MapKeys()
commandNamesSorted := make([]string, len(commandNames))
for i, cmd := range commandNames {
commandNamesSorted[i] = cmd.String()
}
sort.Strings(commandNamesSorted)
return commandNamesSorted
}
func (c *Broccli) sortedEnv() []string {
envNames := reflect.ValueOf(c.env).MapKeys()
envNamesSorted := make([]string, len(envNames))
for i, ev := range envNames {
envNamesSorted[i] = ev.String()
}
sort.Strings(envNamesSorted)
return envNamesSorted
}
func (c *Broccli) printHelp() {
var helpMessage strings.Builder
_, _ = fmt.Fprintf(
&helpMessage,
"%s by %s\n%s\n\nUsage: %s COMMAND\n\n",
c.name,
c.author,
c.usage,
path.Base(os.Args[0]),
)
if len(c.env) > 0 {
_, _ = fmt.Fprintf(&helpMessage, "Required environment variables:\n")
tabFormatter := new(tabwriter.Writer)
tabFormatter.Init(
&helpMessage,
tabWriterMinWidth,
tabWriterTabWidth,
tabWriterPadding,
tabWriterPadChar,
0,
)
for _, n := range c.sortedEnv() {
_, _ = fmt.Fprintf(tabFormatter, "%s\t%s\n", n, c.env[n].usage)
}
_ = tabFormatter.Flush()
}
_, _ = fmt.Fprintf(&helpMessage, "Commands:\n")
tabFormatter := new(tabwriter.Writer)
tabFormatter.Init(
&helpMessage,
tabWriterMinWidthForCommand,
tabWriterTabWidth,
tabWriterPadding,
tabWriterPadChar,
0,
)
for _, commandName := range c.sortedCommands() {
_, _ = fmt.Fprintf(&helpMessage, " %s\t%s\n", commandName, c.commands[commandName].usage)
}
_ = tabFormatter.Flush()
_, _ = fmt.Fprintf(
&helpMessage,
"\nRun '%s COMMAND --help' for command syntax.\n",
path.Base(os.Args[0]),
)
_, err := fmt.Fprint(os.Stdout, helpMessage.String())
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Unable to build help message")
}
}
func (c *Broccli) printInvalidCommand(cmd string) {
fmt.Fprintf(os.Stderr, "Invalid command: %s\n\n", cmd)
c.printHelp()
}
// getFlagSetPtrs creates flagset instance, parses flags and returns list of pointers to results of parsing the flags.
func (c *Broccli) getFlagSetPtrs(
cmd *Command,
) (map[string]interface{}, map[string]interface{}, []string) {
fset := flag.NewFlagSet("flagset", flag.ContinueOnError)
// nothing should come out of flagset
fset.Usage = func() {}
fset.SetOutput(io.Discard)
flagNamePtrs := make(map[string]interface{})
flagAliasPtrs := make(map[string]interface{})
flagNamesSorted := cmd.sortedFlags()
for _, flagName := range flagNamesSorted {
flagInstance := cmd.flags[flagName]
if flagInstance.valueType == TypeBool {
flagNamePtrs[flagName] = fset.Bool(flagName, false, "")
if flagInstance.alias != "" {
flagAliasPtrs[flagInstance.alias] = fset.Bool(flagInstance.alias, false, "")
}
} else {
flagNamePtrs[flagName] = fset.String(flagName, "", "")
if flagInstance.alias != "" {
flagAliasPtrs[flagInstance.alias] = fset.String(flagInstance.alias, "", "")
}
}
}
err := fset.Parse(os.Args[2:])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Unable to parse flags: %s", err.Error())
}
return flagNamePtrs, flagAliasPtrs, fset.Args()
}
func (c *Broccli) checkEnv(cmd *Command) int {
if len(cmd.env) == 0 {
return 0
}
for envName, envVar := range cmd.env {
envValue := os.Getenv(envName)
envVar.flags |= IsRequired
err := envVar.validateValue(envValue)
if err != nil {
fmt.Fprintf(
os.Stderr,
"ERROR: %s %s: %s\n",
c.getParamTypeName(ParamEnvVar),
envVar.name,
err.Error(),
)
cmd.printHelp()
return 1
}
}
return 0
}
func (c *Broccli) processOnTrue(
cmd *Command,
flagNames []string,
nflags map[string]interface{},
aflags map[string]interface{},
) {
for _, name := range flagNames {
if cmd.flags[name].valueType != TypeBool {
continue
}
if cmd.flags[name].options.onTrue == nil {
continue
}
// OnTrue is called when a flag is true
//nolint:forcetypeassert
if *(nflags[name]).(*bool) || *(aflags[cmd.flags[name].alias]).(*bool) {
cmd.flags[name].options.onTrue(cmd)
}
}
}
func (c *Broccli) processFlags(
cmd *Command,
flagNames []string,
nflags map[string]interface{},
aflags map[string]interface{},
) int {
for _, name := range flagNames {
flag := cmd.flags[name]
if flag.valueType == TypeBool {
c.parsedFlags[name] = "false"
//nolint:forcetypeassert
if *(nflags[name]).(*bool) || (cmd.flags[name].alias != "" && *(aflags[cmd.flags[name].alias]).(*bool)) {
c.parsedFlags[name] = "true"
}
continue
}
//nolint:forcetypeassert
aliasValue := ""
if flag.alias != "" {
aliasValue = *(aflags[flag.alias]).(*string)
}
//nolint:forcetypeassert
nameValue := *(nflags[name]).(*string)
if nameValue != "" && aliasValue != "" {
fmt.Fprintf(os.Stderr, "ERROR: Both -%s and --%s passed", flag.alias, flag.name)
return 1
}
flagValue := aliasValue
if nameValue != "" {
flagValue = nameValue
}
err := flag.validateValue(flagValue)
if err != nil {
fmt.Fprintf(
os.Stderr,
"ERROR: %s %s: %s\n",
c.getParamTypeName(ParamFlag),
name,
err.Error(),
)
cmd.printHelp()
return 1
}
c.parsedFlags[name] = flagValue
}
return 0
}
func (c *Broccli) processArgs(cmd *Command, argNamesSorted []string, args []string) int {
for argIdx, argName := range argNamesSorted {
argValue := ""
if len(args) >= argIdx+1 {
argValue = args[argIdx]
}
err := cmd.args[argName].validateValue(argValue)
if err != nil {
fmt.Fprintf(
os.Stderr,
"ERROR: %s %s: %s\n",
c.getParamTypeName(ParamArg),
cmd.args[argName].valuePlaceholder,
err.Error(),
)
cmd.printHelp()
return 1
}
c.parsedArgs[argName] = argValue
}
return 0
}
func (c *Broccli) processOnPostValidation(cmd *Command) int {
if cmd.options.onPostValidation == nil {
return 0
}
err := cmd.options.onPostValidation(cmd)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err.Error())
cmd.printHelp()
return 1
}
return 0
}
func (c *Broccli) parseFlags(cmd *Command) int {
// check required environment variables
if exitCode := c.checkEnv(cmd); exitCode != 0 {
return exitCode
}
flags := cmd.sortedFlags()
flagNamePtrs, flagAliasPtrs, args := c.getFlagSetPtrs(cmd)
// Loop through boolean flags and execute onTrue() hook if exists. That function might be used to change behaviour
// of other flags, eg. when -e is added, another flag or argument might become required (or obsolete).
// Bool fields will be parsed out in this loop so no reason to process them again in the next one.
c.processOnTrue(cmd, flags, flagNamePtrs, flagAliasPtrs)
if exitCode := c.processFlags(cmd, flags, flagNamePtrs, flagAliasPtrs); exitCode != 0 {
return exitCode
}
argsNamesSorted := cmd.sortedArgs()
if exitCode := c.processArgs(cmd, argsNamesSorted, args); exitCode != 0 {
return exitCode
}
if exitCode := c.processOnPostValidation(cmd); exitCode != 0 {
return exitCode
}
return 0
}
func (c *Broccli) getParamTypeName(t int8) string {
if t == ParamArg {
return "Argument"
}
if t == ParamEnvVar {
return "Env var"
}
return "Flag"
}