-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_parse.go
587 lines (517 loc) · 16 KB
/
app_parse.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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package warg
import (
"context"
"fmt"
"os"
"strings"
"go.bbkane.com/warg/command"
"go.bbkane.com/warg/config"
"go.bbkane.com/warg/flag"
"go.bbkane.com/warg/help/common"
"go.bbkane.com/warg/section"
"go.bbkane.com/warg/value"
)
type flagStr struct {
NameOrAlias string
Value string
Consumed bool
}
type gatherArgsResult struct {
// Path holds the path to the current command/section. Does not include the app name
Path []string
// FlagStrs is a slice of flags and values passed from the CLI. It can't be a map because flags can have aliases and we need to preserve order
FlagStrs []flagStr
// HelpPassed records whether --help was passed. The help flag may be set to a default value, so we need to check whether it's passed explicitly
// so we can decide whether it needs to be acted upon
HelpPassed bool
}
func containsString(haystack []string, needle string) bool {
for _, w := range haystack {
if w == needle {
return true
}
}
return false
}
// gatherArgs separates os.Args into a command path, a list of flags and their values from the CLI.
// It also takes note of whether --help was passed. To minimize ambiguitiy between a path element and an optional
// argument to --help, --help must be either not be passed, be the last string passed, or have exactly one value after it.
// See img/warg-gatherArgs-state-machine.png at the root of the repo for a diagram.
func gatherArgs(osArgs []string, helpFlagNames []string) (*gatherArgsResult, error) {
res := &gatherArgsResult{
Path: nil,
FlagStrs: nil,
HelpPassed: false,
}
startSt := "startSt"
helpFlagPassedSt := "helpFlagPassedSt"
helpValuePassedSt := "helpValuePassedSt"
flagPassedSt := "flagPassedSt"
state := startSt
var currentFlagName string
// Skip the name of the executable passed.
for _, arg := range osArgs[1:] {
// fmt.Printf("state: %v, arg: %v\n", state, arg)
switch state {
case startSt:
if containsString(helpFlagNames, arg) {
res.HelpPassed = true
currentFlagName = arg
state = helpFlagPassedSt
} else if strings.HasPrefix(arg, "-") {
currentFlagName = arg
state = flagPassedSt
} else { // cmd
res.Path = append(res.Path, arg)
state = startSt
}
case helpFlagPassedSt:
res.FlagStrs = append(
res.FlagStrs,
flagStr{NameOrAlias: currentFlagName, Value: arg, Consumed: false},
)
state = helpValuePassedSt
case helpValuePassedSt:
return nil, fmt.Errorf("help flags should take maximally one arg, but more than one passed: %s", arg)
case flagPassedSt:
res.FlagStrs = append(
res.FlagStrs,
flagStr{NameOrAlias: currentFlagName, Value: arg, Consumed: false},
)
state = startSt
default:
return nil, fmt.Errorf("internal error: unknown state: %s", state)
}
}
// check the only non-terminal state
if state == flagPassedSt {
return nil, fmt.Errorf("flag passed without value( %#v) . All flags must have one value passed. Repeat flags to accumulate values. Example: --level 9000", currentFlagName)
}
return res, nil
}
// flagNameToAlias is a map of flag name to flag alias
type flagNameToAlias map[flag.Name]flag.Name
// fitToAppResult holds the result of fitToApp
// Exactly one of Section or Command should hold something. The other should be nil
type fitToAppResult struct {
Section *section.SectionT
Command *command.Command
Action command.Action
AllowedFlags flag.FlagMap
AllowedFlagAliases flagNameToAlias
}
// fitToApp takes the command entered by a user and uses it to "walk" down the apps command tree to build what the command was and what the available flags are.
func fitToApp(rootSection section.SectionT, path []string) (*fitToAppResult, error) {
// AllowedFlags grows, as we traverse the tree; copy rootSection.Flags.
// We need a pristine rootsection.Flags for --help printing
allowedFlags := make(flag.FlagMap)
for k, v := range rootSection.Flags {
allowedFlags[k] = v
}
// validate passed command and get available flags
ftar := fitToAppResult{
Section: &rootSection,
Command: nil, // we start with a section, not a command
Action: nil,
AllowedFlags: allowedFlags,
AllowedFlagAliases: make(flagNameToAlias),
}
// Add any root flag aliases to AllowedFlagAliases
// Wonder if I could put all this in one part of the code...
for flagName, fl := range ftar.AllowedFlags {
if fl.Alias != "" {
ftar.AllowedFlagAliases[flagName] = fl.Alias
}
}
childCommands := rootSection.Commands
childSections := rootSection.Sections
for _, word := range path {
if command, exists := childCommands[command.Name(word)]; exists {
ftar.Command = &command
ftar.Section = nil
ftar.Action = command.Action
// once we're in a commmand, we should be at the end of the path
// commands have no child commands or child sections
childCommands = nil
childSections = nil
for flagName, fl := range command.Flags {
// TODO: check if name exists already
if fl.Alias != "" {
ftar.AllowedFlagAliases[flagName] = fl.Alias
}
fl.IsCommandFlag = true
ftar.AllowedFlags[flagName] = fl
}
} else if section, exists := childSections[section.Name(word)]; exists {
ftar.Section = §ion
childCommands = section.Commands
childSections = section.Sections
for flagName, fl := range section.Flags {
// TODO: check if key exists already
if fl.Alias != "" {
ftar.AllowedFlagAliases[flagName] = fl.Alias
}
ftar.AllowedFlags[flagName] = fl
}
} else {
retErr := fmt.Errorf("expected command or section, but got %#v, try --help", word)
return nil, retErr
}
}
return &ftar, nil
}
// resolveFlag updates a flag's value from the command line, and then from the
// default value. flag should not be nil. deletes from flagStrs
func resolveFlag(
fl *flag.Flag,
name flag.Name,
flagStrs []flagStr,
configReader config.Reader,
lookupEnv LookupFunc,
aliases flagNameToAlias,
) error {
// TODO: can I delete from flagStrs in the caller? then I wouldn't need to pass
// flagStrs (just a potential strValues) into here and it's a more pure function
val, err := fl.EmptyValueConstructor()
if err != nil {
return fmt.Errorf("flag error: %v: %w", name, err)
}
fl.Value = val
// try to update from command line and consume from flagStrs
// need to check flag.SetBy even in the first case because we could be resolving
// flags multiple times (for instance --config gets resolved before this and also now)
{
strValues := []string{}
for i := range flagStrs {
// TODO: come back to theses string casts...
if flagStrs[i].NameOrAlias == string(name) || flag.Name(flagStrs[i].NameOrAlias) == aliases[name] {
strValues = append(strValues, flagStrs[i].Value)
flagStrs[i].Consumed = true
}
}
if fl.SetBy == "" && len(strValues) > 0 {
_, isScalar := val.(value.ScalarValue)
if isScalar && len(strValues) > 1 {
return fmt.Errorf("flag error: %v: flag passed multiple times, it's value (type %v), can only be updated once", name, fl.Value.Description())
}
for _, v := range strValues {
// Unset the value if we get UnsetSentinel
if v == fl.UnsetSentinel {
val, err := fl.EmptyValueConstructor()
if err != nil {
return fmt.Errorf("flag error: %v: %w", name, err)
}
fl.Value = val
// Set to "unsetSentinel" to avoid updating from config etc..
// This will be set back to "" at end of update
fl.SetBy = "unsetSentinel"
continue
}
err = fl.Value.Update(v)
if err != nil {
return fmt.Errorf("error updating flag %v from passed flag value %v: %w", name, v, err)
}
fl.SetBy = "passedflag"
}
}
}
// update from config
{
if fl.SetBy == "" && configReader != nil {
fpr, err := configReader.Search(fl.ConfigPath)
if err != nil {
return err
}
if fpr != nil {
if !fpr.IsAggregated {
err := fl.Value.ReplaceFromInterface(fpr.IFace)
if err != nil {
return fmt.Errorf(
"could not replace container type value:\nval:\n%#v\nreplacement:\n%#v\nerr: %w",
fl.Value,
fpr.IFace,
err,
)
}
fl.SetBy = "config"
} else {
v, ok := fl.Value.(value.SliceValue)
if !ok {
return fmt.Errorf("could not update scalar value with aggregated value from config: name: %v, configPath: %v", name, fl.ConfigPath)
}
under, ok := fpr.IFace.([]interface{})
if !ok {
return fmt.Errorf("expected []interface{}, got: %#v", under)
}
for _, e := range under {
err = v.AppendFromInterface(e)
if err != nil {
return fmt.Errorf("could not update container type value: err: %w", err)
}
}
fl.SetBy = "config"
fl.Value = v
}
}
}
}
// update from envvars
{
if fl.SetBy == "" && len(fl.EnvVars) > 0 {
for _, e := range fl.EnvVars {
val, exists := lookupEnv(e)
if exists {
err = fl.Value.Update(val)
if err != nil {
return fmt.Errorf("error updating flag %v from envvar %v: %w", name, val, err)
}
fl.SetBy = "envvar"
break // stop looking for envvars
}
}
}
}
// update from default
{
if fl.SetBy == "" && fl.Value.HasDefault() {
fl.Value.ReplaceFromDefault()
fl.SetBy = "appdefault"
}
}
// Set to "" if unsetSentinel
if fl.SetBy == "unsetSentinel" {
fl.SetBy = ""
}
return nil
}
// ParseResult holds the result of parsing the command line.
type ParseResult struct {
Context command.Context
// Action holds the passed command's action to execute.
Action command.Action
}
type ParseOptHolder struct {
Args []string
Context context.Context
LookupFunc LookupFunc
// Stderr will be passed to command.Context for user commands to print to.
// This file is never closed by warg, so if setting to something other than stderr/stdout,
// remember to close the file after running the command.
// Useful for saving output for tests. Defaults to os.Stderr if not passed
Stderr *os.File
// Stdout will be passed to command.Context for user commands to print to.
// This file is never closed by warg, so if setting to something other than stderr/stdout,
// remember to close the file after running the command.
// Useful for saving output for tests. Defaults to os.Stdout if not passed
Stdout *os.File
}
type ParseOpt func(*ParseOptHolder)
func AddContext(ctx context.Context) ParseOpt {
return func(poh *ParseOptHolder) {
poh.Context = ctx
}
}
func OverrideArgs(args []string) ParseOpt {
return func(poh *ParseOptHolder) {
poh.Args = args
}
}
func OverrideLookupFunc(lookup LookupFunc) ParseOpt {
return func(poh *ParseOptHolder) {
poh.LookupFunc = lookup
}
}
func OverrideStderr(stderr *os.File) ParseOpt {
return func(poh *ParseOptHolder) {
poh.Stderr = stderr
}
}
func OverrideStdout(stdout *os.File) ParseOpt {
return func(poh *ParseOptHolder) {
poh.Stdout = stdout
}
}
func NewParseOptHolder(opts ...ParseOpt) ParseOptHolder {
parseOptHolder := ParseOptHolder{
Context: nil,
Args: nil,
LookupFunc: nil,
Stderr: nil,
Stdout: nil,
}
for _, opt := range opts {
opt(&parseOptHolder)
}
if parseOptHolder.Args == nil {
OverrideArgs(os.Args)(&parseOptHolder)
}
if parseOptHolder.Context == nil {
AddContext(context.Background())(&parseOptHolder)
}
if parseOptHolder.LookupFunc == nil {
OverrideLookupFunc(os.LookupEnv)(&parseOptHolder)
}
if parseOptHolder.Stderr == nil {
OverrideStderr(os.Stderr)(&parseOptHolder)
}
if parseOptHolder.Stdout == nil {
OverrideStdout(os.Stdout)(&parseOptHolder)
}
return parseOptHolder
}
func (app *App) parseWithOptHolder(parseOptHolder ParseOptHolder) (*ParseResult, error) {
osArgs := parseOptHolder.Args
osLookupEnv := parseOptHolder.LookupFunc
helpFlagNames := []string{string(app.helpFlagName)}
if app.helpFlagAlias != "" {
helpFlagNames = append(helpFlagNames, string(app.helpFlagAlias))
}
gar, err := gatherArgs(osArgs, helpFlagNames)
if err != nil {
return nil, err
}
ftar, err := fitToApp(app.rootSection, gar.Path)
if err != nil {
return nil, err
}
// fill the flags
var configReader config.Reader
// get the value of a potential passed --config flag first so we can use it
// to resolve further flags
if app.configFlag != nil {
// Maybe this should go in fitToApp?
if app.configFlag.Alias != "" {
ftar.AllowedFlagAliases[app.configFlagName] = app.configFlag.Alias
}
// we're gonna make a config map out of this if everything goes well
// so pass nil for the configreader now
err = resolveFlag(
app.configFlag,
app.configFlagName,
gar.FlagStrs,
nil,
osLookupEnv,
ftar.AllowedFlagAliases,
)
if err != nil {
return nil, err
}
// NOTE: this *should* always be a string
configPath := app.configFlag.Value.Get().(string)
configReader, err = app.newConfigReader(configPath)
if err != nil {
return nil, fmt.Errorf("error reading config path ( %s ) : %w", configPath, err)
}
}
// Loop over allowed flags for the passed command and try to resolve them
for name, fl := range ftar.AllowedFlags {
err = resolveFlag(
&fl,
name,
gar.FlagStrs,
configReader,
osLookupEnv,
ftar.AllowedFlagAliases,
)
if err != nil {
return nil, err
}
if !gar.HelpPassed {
if fl.Required && fl.SetBy == "" {
return nil, fmt.Errorf("flag required but not set: %s", name)
}
}
ftar.AllowedFlags[name] = fl
}
// add the config flag so both help and actions can see it
if app.configFlag != nil {
ftar.AllowedFlags[app.configFlagName] = *app.configFlag
}
for _, e := range gar.FlagStrs {
if !e.Consumed {
return nil, fmt.Errorf("unrecognized flag: %v -> %v", e.NameOrAlias, e.Value)
}
}
pfs := make(command.PassedFlags)
for name, fl := range ftar.AllowedFlags {
if fl.SetBy != "" {
pfs[string(name)] = fl.Value.Get()
}
}
// OK! Let's make the ParseResult for each case and gtfo
if ftar.Section != nil && ftar.Command == nil {
// no legit actions, just print the help
helpInfo := common.HelpInfo{
AvailableFlags: ftar.AllowedFlags,
RootSection: app.rootSection,
}
// We know the helpFlag has a default so this is safe
helpType := ftar.AllowedFlags[flag.Name(app.helpFlagName)].Value.Get().(string)
for _, e := range app.helpMappings {
if e.Name == helpType {
pr := ParseResult{
Context: command.Context{
AppName: app.name,
Context: parseOptHolder.Context,
Flags: pfs,
Path: gar.Path,
Stderr: parseOptHolder.Stderr,
Stdout: parseOptHolder.Stdout,
Version: app.version,
},
Action: e.SectionHelp(ftar.Section, helpInfo),
}
return &pr, nil
}
}
return nil, fmt.Errorf("some problem with section help: info: %v", helpInfo)
} else if ftar.Section == nil && ftar.Command != nil {
if gar.HelpPassed {
helpInfo := common.HelpInfo{
AvailableFlags: ftar.AllowedFlags,
RootSection: app.rootSection,
}
// We know the helpFlag has a default so this is safe
helpType := ftar.AllowedFlags[flag.Name(app.helpFlagName)].Value.Get().(string)
for _, e := range app.helpMappings {
if e.Name == helpType {
pr := ParseResult{
Context: command.Context{
AppName: app.name,
Context: parseOptHolder.Context,
Flags: pfs,
Path: gar.Path,
Stderr: parseOptHolder.Stderr,
Stdout: parseOptHolder.Stdout,
Version: app.version,
},
Action: e.CommandHelp(ftar.Command, helpInfo),
}
return &pr, nil
}
}
return nil, fmt.Errorf("some problem with section help: info: %v", helpInfo)
} else {
pr := ParseResult{
Context: command.Context{
AppName: app.name,
Context: parseOptHolder.Context,
Flags: pfs,
Path: gar.Path,
Stderr: parseOptHolder.Stderr,
Stdout: parseOptHolder.Stdout,
Version: app.version,
},
Action: ftar.Action,
}
return &pr, nil
}
} else {
return nil, fmt.Errorf("internal Error: invalid parse state: currentSection == %v, currentCommand == %v", ftar.Section, ftar.Command)
}
}
// Parse parses the args, but does not execute anything.
func (app *App) Parse(opts ...ParseOpt) (*ParseResult, error) {
parseOptHolder := NewParseOptHolder(opts...)
return app.parseWithOptHolder(parseOptHolder)
}