-
Notifications
You must be signed in to change notification settings - Fork 20
/
cmd.go
69 lines (58 loc) · 2.05 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
package cmd
import (
"fmt"
"os"
"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// 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":
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(gCurrentAccount.DefaultZone) // nolint:errcheck
}
}
func cmdExitOnUsageError(cmd *cobra.Command, reason string) {
cmd.PrintErrln(fmt.Sprintf("error: %s", reason))
cmd.Usage() // nolint:errcheck
os.Exit(1)
}
// 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
}