-
Notifications
You must be signed in to change notification settings - Fork 18
/
cmd.go
591 lines (497 loc) · 16.5 KB
/
cmd.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
588
589
590
591
package cmd
import (
"bytes"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
"github.com/hashicorp/go-multierror"
"github.com/iancoleman/strcase"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// cliCommandImplemError represents an implementation error for a cliCommand.
type cliCommandImplemError struct {
reason string
}
// Error returns a string representation of the cliCommandImplemError.
func (e cliCommandImplemError) Error() string {
return fmt.Sprintf(
"CLI command implementation error: %s. "+
"This is a bug, and should be reported to the maintainers of this tool.",
e.reason)
}
const cmdFlagForceHelp = "attempt to perform the operation without prompting for confirmation"
// cmdCheckRequiredFlags evaluates the specified flags as parsed in the cobra.Command flagset to check that
// their value is unset (i.e. null/empty/zero, depending on the type), and returns a multierror listing all
// flags missing a required value.
func cmdCheckRequiredFlags(cmd *cobra.Command, flags []string) error {
var err *multierror.Error
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
for _, fn := range flags {
if flag.Name == fn {
var hasValue bool
switch flag.Value.Type() {
case "string", "stringSlice":
if flag.Value.String() != "" {
hasValue = true
}
case "int", "uint", "int8", "uint8", "int16", "uint16", "int32", "uint32", "int64", "uint64",
"float32", "float64":
if flag.Value.String() != "0" {
hasValue = true
}
}
if !hasValue {
err = multierror.Append(err, fmt.Errorf("no value specified for flag %q", fn))
}
}
}
})
return err.ErrorOrNil()
}
// cmdSetZoneFlagFromDefault attempts to set the "--zone" flag value based on the current active account's
// default zone setting if set. This is a convenience helper, there is no guarantee that the flag will be
// set once this function returns.
func cmdSetZoneFlagFromDefault(cmd *cobra.Command) {
if cmd.Flag("zone").Value.String() == "" {
cmd.Flag("zone").Value.Set(account.CurrentAccount.DefaultZone) // nolint:errcheck
}
}
// cmdSetTemplateFlagFromDefault attempts to set the "--template" flag value based on the current active account's
// default template setting if set. This is a convenience helper, there is no guarantee that the flag will be
// set once this function returns.
func cmdSetTemplateFlagFromDefault(cmd *cobra.Command) {
if cmd.Flag("template").Value.String() == "" {
cmd.Flag("template").Value.Set(account.CurrentAccount.DefaultTemplate) // nolint:errcheck
}
}
func cmdExitOnUsageError(cmd *cobra.Command, reason string) {
cmd.PrintErrln(fmt.Sprintf("error: %s", reason))
cmd.Usage() // nolint:errcheck
os.Exit(1)
}
// cmdShowHelpFlags outputs flags matching the specified prefix in the provided flag set.
// This can be used for example to craft specialized usage help messages for hidden flags.
func cmdShowHelpFlags(flags *pflag.FlagSet, prefix string) {
buf := bytes.NewBuffer(nil)
t := table.NewEmbeddedTable(buf)
flags.VisitAll(func(flag *pflag.Flag) {
if strings.HasPrefix(flag.Name, prefix) {
t.Append([]string{"--" + flag.Name, flag.Usage})
}
})
t.Render()
fmt.Print(buf)
}
// completeVMNames is a Cobra Command.ValidArgsFunction that returns the list of Compute instance names belonging to
// the current user for shell auto-completion.
func completeVMNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
list, err := listVMs()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return list.(*vmListOutput).names(), cobra.ShellCompDirectiveNoFileComp
}
func getCommaflag(p string) []string {
if p == "" {
return nil
}
p = strings.Trim(p, ",")
args := strings.Split(p, ",")
res := []string{}
for _, arg := range args {
if arg == "" {
continue
}
res = append(res, strings.TrimSpace(arg))
}
return res
}
// cliCommandSettings represents a CLI command settings.
type cliCommandSettings struct {
outputFunc func(o output.Outputter, err error) error
}
// defaultCLICmdSettings returns a cliCommandSettings struct initialized
// with default values.
func defaultCLICmdSettings() cliCommandSettings {
return cliCommandSettings{
outputFunc: printOutput,
}
}
// cliCommand is the interface to implement for leveraging the automatic CLI
// command generation system based on implementer struct tags.
//
// For reference, implementers can look up the unit tests (testCLICmd struct).
// By default, all struct fields are parsed for tags: if you have private
// fields used for internal purpose, set the tag `cli:"-"` on those to exclude
// them from the CLI command evaluation process.
//
// Note: this system is an attempt at reducing the amount of boilerplate code
// required to implement CLI commands, and pagmatically supports only the
// most common CLI flag types used across the codebase (e.g. for simple CRUD
// commands). It is not one-size-fits-all and doesn't strive to be: if as a
// CLI command implementer you hit a limitation in a use case more complex
// than usual, you always have the option to use vanilla cobra/pflags, which
// is certainly easier than try to implement the missing functionnality in
// this "framework".
type cliCommand interface {
cmdAliases() []string
cmdShort() string
cmdLong() string
cmdPreRun(*cobra.Command, []string) error
cmdRun(*cobra.Command, []string) error
}
// cliCommandFlagName returns the CLI flag name corresponding to the field
// specified from the cliCommand.
func cliCommandFlagName(c cliCommand, field interface{}) (string, error) {
fieldValue := reflect.ValueOf(field)
if fieldValue.Kind() != reflect.Ptr || fieldValue.IsNil() {
return "", fmt.Errorf("field must be a non-nil pointer value")
}
cv := reflect.ValueOf(c).Elem()
for i := 0; i < cv.NumField(); i++ {
structField := cv.Type().Field(i)
if cv.Field(i).UnsafeAddr() == fieldValue.Pointer() {
flagName := strcase.ToKebab(structField.Name)
if v, ok := structField.Tag.Lookup("cli-flag"); ok {
flagName = v
}
return flagName, nil
}
}
return "", fmt.Errorf("field not found in struct %s", cv.Type())
}
func mustCLICommandFlagName(c cliCommand, field interface{}) string {
v, err := cliCommandFlagName(c, field)
if err != nil {
panic(cliCommandImplemError{fmt.Sprintf("cliCommandFlagName: %s", err)})
}
return v
}
// cliCommandFlagSet generates a pflag.FlagSet struct from the specified
// cliCommand struct tags. Supported tags are:
// - cli-flag:"<flag name>": override the flag name derived by default from
// the struct field name (e.g.: cliCommand.SomeArg -> "--some-arg").
// - cli-short:"<character>": an optional short version of the flag, e.g.
// Zone string `cli-short:"z"` generates the CLI flag "--zone, -z".
// - cli-usage:"<usage help>": an optional string to use as flag usage
// help message. For positional arguments, this field is used as argument
// label for the "use" command help.
// - cli-hidden:"": mark the corresponding flag "hidden".
func cliCommandFlagSet(c cliCommand) (*pflag.FlagSet, error) {
fs := pflag.NewFlagSet("", pflag.ExitOnError)
cv := reflect.ValueOf(c)
if cv.Kind() == reflect.Ptr {
cv = cv.Elem()
}
for i := 0; i < cv.NumField(); i++ {
cTypeField := cv.Type().Field(i)
if v, ok := cTypeField.Tag.Lookup("cli"); ok && v == "-" {
continue
}
if _, ok := cTypeField.Tag.Lookup("cli-cmd"); ok {
continue
}
if _, ok := cTypeField.Tag.Lookup("cli-arg"); ok {
continue
}
flagName := strcase.ToKebab(cTypeField.Name)
if v, ok := cTypeField.Tag.Lookup("cli-flag"); ok {
flagName = v
}
flagShort := ""
if v, ok := cTypeField.Tag.Lookup("cli-short"); ok {
flagShort = v
}
flagUsage := ""
if v, ok := cTypeField.Tag.Lookup("cli-usage"); ok {
flagUsage = v
}
flagDefaultValue := cv.Field(i).Interface()
switch t := cTypeField.Type.Kind(); t {
case reflect.String:
fs.StringP(flagName, flagShort, flagDefaultValue.(string), flagUsage)
case reflect.Int64:
fs.Int64P(flagName, flagShort, flagDefaultValue.(int64), flagUsage)
case reflect.Bool:
fs.BoolP(flagName, flagShort, flagDefaultValue.(bool), flagUsage)
case reflect.Slice:
if cTypeField.Type.Elem().Kind() != reflect.String {
return nil, cliCommandImplemError{
fmt.Sprintf("unsupported type []%s for field %s.%s", t, cv.Type(), cTypeField.Name),
}
}
fs.StringSliceP(flagName, flagShort, flagDefaultValue.([]string), flagUsage)
case reflect.Map:
if cTypeField.Type.Elem().Kind() != reflect.String {
return nil, cliCommandImplemError{
fmt.Sprintf(
"unsupported type map[string]%s for field %s.%s",
t,
cv.Type(),
cTypeField.Name,
),
}
}
fs.StringToStringP(flagName, flagShort, flagDefaultValue.(map[string]string), flagUsage)
default:
return nil, cliCommandImplemError{fmt.Sprintf("unsupported type %s", t)}
}
if _, ok := cTypeField.Tag.Lookup("cli-hidden"); ok {
if err := fs.MarkHidden(flagName); err != nil {
return nil, cliCommandImplemError{
reason: fmt.Sprintf("error marking flag %q hidden: %v", flagName, err),
}
}
}
}
return fs, nil
}
// cliCommandUse generates a string to be used as value for the cobra.Command
// "Use" field from the specified cliCommand struct tags. Supported tags are:
// - cli-cmd:"<command name>": the name of the command (required).
// - cli-usage:"<usage help>": an optional string to use as argument label
// for the "use" command help.
// - cli-arg:"<p>": declare a command line positional argument. Depending
// on the type of the structure field (string or []string), the value of
// <p> can either be "#" to declare a single argument which position
// matches the one of the corresponding *ARGUMENT field* in the struct
// type definition, or "?" to declare an optional single argument. If the
// struct field is a []string, the result is a variadic (i.e. 0 or more)
// list of remaining arguments; if "cli-arg:"?"` is specified, the list
// will be marked as optional in the "use" command help.
func cliCommandUse(c cliCommand) (string, error) {
var (
commandName string
use = make([]string, 0)
)
cv := reflect.ValueOf(c)
if cv.Kind() == reflect.Ptr {
cv = cv.Elem()
}
for i := 0; i < cv.NumField(); i++ {
cTypeField := cv.Type().Field(i)
if v, ok := cv.Type().Field(i).Tag.Lookup("cli-cmd"); ok {
commandName = v
continue
}
if v, ok := cTypeField.Tag.Lookup("cli-arg"); ok {
argLabel := strings.ToUpper(strcase.ToKebab(cv.Type().Field(i).Name))
if u, ok := cTypeField.Tag.Lookup("cli-usage"); ok {
argLabel = u
}
switch cTypeField.Type.Kind() {
case reflect.Int64, reflect.String:
if v == "?" {
use = append(use, "["+argLabel+"]")
} else {
use = append(use, argLabel)
}
case reflect.Slice:
if cTypeField.Type.Elem().Kind() != reflect.String {
return "", cliCommandImplemError{fmt.Sprintf(
"unsupported type []%s for field %s.%s",
cTypeField.Type.Elem().Kind(),
cv.Type(),
cTypeField.Name,
)}
}
if v == "?" {
use = append(use, "["+argLabel+"]...")
} else {
use = append(use, argLabel+"...")
}
default:
return "", cliCommandImplemError{fmt.Sprintf(
"unsupported type %s on field %s.%s",
cTypeField.Type.Kind(),
cv.Type(),
cTypeField.Name,
)}
}
}
}
if commandName == "" {
return "", cliCommandImplemError{
fmt.Sprintf("`cli-cmd` tag missing from struct %s", cv.Type()),
}
}
use = append([]string{commandName}, use...)
return strings.Join(use, " "), nil
}
// cliCommandDefaultPreRun is a convenience helper function that can be used
// in cliCommand.cmdPreRun() hooks to automagically retrieve values for the
// struct flags/args fields from a cobra.Command and args provided, and set
// corresponding fields on the struct implementing the cliCommand interface.
func cliCommandDefaultPreRun(c cliCommand, cmd *cobra.Command, args []string) error {
cv := reflect.ValueOf(c)
if cv.Kind() == reflect.Ptr {
cv = cv.Elem()
}
argp := 0
for i := 0; i < cv.NumField(); i++ {
cField := cv.Field(i)
cTypeField := cv.Type().Field(i)
if v, ok := cTypeField.Tag.Lookup("cli"); ok && v == "-" {
continue
}
if _, ok := cTypeField.Tag.Lookup("cli-cmd"); ok {
continue
}
// Positional args handling:
if argMode, ok := cTypeField.Tag.Lookup("cli-arg"); ok {
switch t := cTypeField.Type.Kind(); t {
case reflect.Int64:
if argMode == "#" {
// Required arg
if argp >= len(args) {
return fmt.Errorf("missing arguments, run with --help for usage")
}
argVal, err := strconv.Atoi(args[argp])
if err != nil {
return fmt.Errorf("invalid value %q", args[argp])
}
cField.SetInt(int64(argVal))
} else if argMode == "?" {
// Optional arg
if argp < len(args) {
argVal, err := strconv.Atoi(args[argp])
if err != nil {
return fmt.Errorf("invalid value %q", args[argp])
}
cField.SetInt(int64(argVal))
}
}
case reflect.String:
if argMode == "#" {
// Required arg
if argp >= len(args) {
return fmt.Errorf("missing arguments, run with --help for usage")
}
cField.SetString(args[argp])
} else if argMode == "?" {
// Optional arg
if argp < len(args) {
cField.SetString(args[argp])
}
}
case reflect.Slice:
if cTypeField.Type.Elem().Kind() != reflect.String {
return cliCommandImplemError{fmt.Sprintf(
"unsupported type []%s for field %s.%s",
cTypeField.Type.Elem().Kind(),
cv.Type(),
cTypeField.Name,
)}
}
if argp < len(args) {
cField.Set(reflect.ValueOf(args[argp:]))
}
default:
return cliCommandImplemError{fmt.Sprintf(
"unsupported type %s on field %s.%s", t, cv.Type(), cTypeField.Name,
)}
}
argp++
continue
}
// Optional flags handling:
flagName := strcase.ToKebab(cv.Type().Field(i).Name)
if v, ok := cTypeField.Tag.Lookup("cli-flag"); ok {
flagName = v
}
if cmd.Flags().Lookup(flagName) == nil {
return cliCommandImplemError{fmt.Sprintf(
"flag --%s not declared for field %s.%s",
flagName,
cv.Type(),
cv.Type().Field(i).Name,
)}
}
switch t := cTypeField.Type.Kind(); t {
case reflect.String:
v, err := cmd.Flags().GetString(flagName)
if err != nil {
return fmt.Errorf("error retrieving value for flag --%s: %s", flagName, err)
}
cField.SetString(v)
case reflect.Int64:
v, err := cmd.Flags().GetInt64(flagName)
if err != nil {
return fmt.Errorf("error retrieving value for flag --%s: %s", flagName, err)
}
cField.SetInt(v)
case reflect.Bool:
v, err := cmd.Flags().GetBool(flagName)
if err != nil {
return fmt.Errorf("error retrieving value for flag %s: --%s", flagName, err)
}
cField.SetBool(v)
case reflect.Slice:
if cv.Type().Field(i).Type.Elem().Kind() != reflect.String {
return cliCommandImplemError{
fmt.Sprintf(
"unsupported type []%s for field %s.%s",
cv.Type().Field(i).Type.Elem().Kind(),
cv.Type(),
cv.Type().Field(i).Name),
}
}
v, err := cmd.Flags().GetStringSlice(flagName)
if err != nil {
return fmt.Errorf("error retrieving value for flag %s: %s", flagName, err)
}
cField.Set(reflect.ValueOf(v))
case reflect.Map:
if cv.Type().Field(i).Type.Elem().Kind() != reflect.String {
return cliCommandImplemError{
fmt.Sprintf(
"unsupported type map[string]%s for field %s.%s",
cv.Type().Field(i).Type.Elem().Kind(),
cv.Type(),
cv.Type().Field(i).Name),
}
}
v, err := cmd.Flags().GetStringToString(flagName)
if err != nil {
return fmt.Errorf("error retrieving value for flag %s: %s", flagName, err)
}
cField.Set(reflect.ValueOf(v))
default:
return cliCommandImplemError{fmt.Sprintf("unsupported type %s", t)}
}
}
return nil
}
// registerCLICommand registers the specified cliCommand instance to the
// current CLI framework (currently Cobra).
func registerCLICommand(parent *cobra.Command, c cliCommand) error {
cmdUse, err := cliCommandUse(c)
if err != nil {
return fmt.Errorf("error initializing CLI command: %s", err)
}
cmd := &cobra.Command{
Use: cmdUse,
Aliases: c.cmdAliases(),
Short: c.cmdShort(),
Long: c.cmdLong(),
PreRunE: c.cmdPreRun,
RunE: c.cmdRun,
}
cmdFlags, err := cliCommandFlagSet(c)
if err != nil {
return fmt.Errorf("error initializing CLI command: %s", err)
}
if cmdFlags != nil {
cmdFlags.VisitAll(func(flag *pflag.Flag) {
cmd.Flags().AddFlag(flag)
})
}
parent.AddCommand(cmd)
return nil
}