/
global_flags.go
107 lines (89 loc) · 3.59 KB
/
global_flags.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
package cmd
import (
"fmt"
"github.com/spf13/viper"
"os"
"strings"
)
const flagApiServer = "api-server"
const flagApiServerKey = "apiServer"
var apiServer string
var flagApiServerValue string
const flagApiClientid = "api-clientid"
const flagApiClientidKey = "apiClientid"
var apiClientid string
var flagApiClientidValue string
const flagApiSecret = "api-secret"
const flagApiSecretKey = "apiSecret"
var apiSecret string
var flagApiSecretValue string
const flagConfig = "config"
var configFile string
var flagConfigValue string
const flagNoConfig = "no-config"
var flagNoConfigValue bool
const flagFormat = "format"
const flagFormatKey = "format"
var format string
var flagFormatValue string
const flagDebug = "debug"
const flagDebugKey = "debug"
var debug bool
var flagDebugValue bool
const flagDryrun = "dryrun"
const flagDryrunKey = "dryrun"
var dryrun bool
var flagDryrunValue bool
func loadGlobalFlags() {
debug = viper.GetBool(flagDebugKey)
if debug {
if configFile == "" {
_, _ = fmt.Fprintf(os.Stderr, "Not using config file\n")
} else {
_, _ = fmt.Fprintf(os.Stderr, "Using config file: %s\n", configFile)
}
}
numFailures := 0
apiServer = strings.TrimSpace(viper.GetString(flagApiServerKey))
apiClientid = strings.TrimSpace(viper.GetString(flagApiClientidKey))
apiSecret = strings.TrimSpace(viper.GetString(flagApiSecretKey))
if apiServer == "" {
apiServer = "https://cloudcli.cloudwm.com"
}
if debug {
_, _ = fmt.Fprintf(os.Stderr, "%s = %s\n", flagApiServerKey, apiServer)
_, _ = fmt.Fprintf(os.Stderr, "%s = %s\n", flagApiClientidKey, apiClientid)
_, _ = fmt.Fprintf(os.Stderr, "%s = %s\n", flagApiSecretKey, apiSecret)
}
format = strings.TrimSpace(viper.GetString(flagFormatKey))
if format != "" && format != "json" && format != "yaml" && format != "human" {
numFailures += 1
fmt.Printf("ERROR: Unsupported --%s flag value: %s\n", flagFormatKey, format)
} else if debug {
_, _ = fmt.Fprintf(os.Stderr, "%s = %s\n", flagFormatKey, format)
}
dryrun = viper.GetBool(flagDryrunKey)
if dryrun {
_, _ = fmt.Fprintf(os.Stderr, "Running in dry run mode, not performing actions\n")
}
if numFailures > 0 {
os.Exit(1)
}
}
func addGlobalFlags() {
rootCmd.PersistentFlags().StringVar(&flagApiServerValue, flagApiServer, "", "API Server Hostname")
_ = viper.BindPFlag(flagApiServerKey, rootCmd.PersistentFlags().Lookup(flagApiServer))
rootCmd.PersistentFlags().Lookup(flagApiServer).Hidden = true
rootCmd.PersistentFlags().StringVar(&flagApiClientidValue, flagApiClientid, "", "API Client ID")
_ = viper.BindPFlag(flagApiClientidKey, rootCmd.PersistentFlags().Lookup(flagApiClientid))
rootCmd.PersistentFlags().StringVar(&flagApiSecretValue, flagApiSecret, "", "API Secret")
_ = viper.BindPFlag(flagApiSecretKey, rootCmd.PersistentFlags().Lookup(flagApiSecret))
rootCmd.PersistentFlags().StringVar(&flagConfigValue, flagConfig, "", "config file (default is $HOME/.cloudcli.yaml)")
rootCmd.PersistentFlags().BoolVar(&flagNoConfigValue, flagNoConfig, false, "disable loading from config file")
rootCmd.PersistentFlags().StringVar(&flagFormatValue, flagFormat, "", "output format, default format is a human readable summary")
_ = viper.BindPFlag(flagFormatKey, rootCmd.PersistentFlags().Lookup(flagFormat))
rootCmd.PersistentFlags().BoolVar(&flagDebugValue, flagDebug, false, "enable debug output to stderr")
_ = viper.BindPFlag(flagDebugKey, rootCmd.PersistentFlags().Lookup(flagDebug))
rootCmd.PersistentFlags().BoolVar(&flagDryrunValue, flagDryrun, false, "enable dry run mode, does not perform actions")
_ = viper.BindPFlag(flagDryrunKey, rootCmd.PersistentFlags().Lookup(flagDryrun))
}