-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmd.go
executable file
·149 lines (139 loc) · 6.04 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
package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"net/url"
)
// Global Parameter
var (
cmdOptions Command
)
// Root command line options
type Command struct {
Debug bool
Environment string
Port int
OpsManHostname string
OpsManUsername string
OpsManPassword string
OpsManClientID string
OpsManClientSecret string
Interval int
SkipSsl bool
CACertFile string
}
// Defaults
const (
defaultInterval = 86400 // 1 day
defaultPort = 8080
envDebug = "DEBUG"
envSslSkip = "SKIP_SSL_VALIDATION"
envInterval = "INTERVAL"
envPort = "PORT"
envOpsManUrl = "OPSMAN_URL"
envOpsManUserName = "OPSMAN_USERNAME"
envOpsManPassword = "OPSMAN_PASSWORD"
envOpsManClientID = "OPSMAN_CLIENT_ID"
envOpsManClientSecret = "OPSMAN_CLIENT_SECRET"
envEnvironment = "ENVIRONMENT"
envCaCertFile = "CACERTFILE"
)
// The root commands.
var rootCmd = &cobra.Command{
Use: fmt.Sprintf("%s", programName),
Short: "VmWare Tanzu Certificate Exporter",
Long: "This application is designed to extract the certificate information from " +
"vmware tanzu operation manager and for prometheus to scrape.",
Version: programVersion,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Before running any command setup the logger log level
initLogger(cmdOptions.Debug)
// Define defaults for arguments that are missing or error out for which there
// is no error
setDefaultsOrErrorIfMissing()
},
Run: func(cmd *cobra.Command, args []string) {
startHttpServer()
},
}
// Set defaults of values of fields that are missed by the user
func setDefaultsOrErrorIfMissing() {
suffixText := "is a required flag and its missing"
if cmdOptions.Interval == 0 { // Interval
cmdOptions.Interval = defaultInterval
}
if cmdOptions.Port == 0 { // Port
cmdOptions.Port = defaultPort
}
if cmdOptions.OpsManHostname == "" { // ops man url
Fatalf("Operation manager URL %s", suffixText)
}
if cmdOptions.Environment == "" { // environment
Fatalf("Environment %s", suffixText)
}
if !cmdOptions.SkipSsl && cmdOptions.CACertFile == "" { // ca cert file is missing
Fatalf("CA cert file parameter %s", suffixText)
}
if cmdOptions.OpsManHostname != "" { // validate ops man URL
u, err := url.ParseRequestURI(cmdOptions.OpsManHostname)
if err != nil {
Fatalf("Invalid URL (%s), err: %v", cmdOptions.OpsManHostname, err)
}
cmdOptions.OpsManHostname = u.Host
}
authenticationChecker(suffixText)
}
// Check what kind of authentication is being used.
func authenticationChecker(msg string) {
if cmdOptions.OpsManUsername == "" && cmdOptions.OpsManClientID == "" { // ops man username
Fatalf("Operation manager username or client ID %s", msg)
}
if cmdOptions.OpsManPassword == "" && cmdOptions.OpsManClientID == "" { // ops man password
Fatalf("Operation manager password or client secret %s", msg)
}
if cmdOptions.OpsManClientID == "" && cmdOptions.OpsManUsername == "" { // client id
Fatalf("Operation manager client id or username %s", msg)
}
if cmdOptions.OpsManClientSecret == "" && cmdOptions.OpsManUsername == "" { // ops man password
Fatalf("Operation manager client secret or password %s", msg)
}
if cmdOptions.OpsManUsername != "" && cmdOptions.OpsManClientID != "" { // cannot have both
Fatalf("choose either username / password or client id / client secret, cannot have both of them together")
}
}
// Initialize the cobra command line
func init() {
// Load the environment variable using viper
viper.AutomaticEnv()
// Root command flags
rootCmd.PersistentFlags().BoolVarP(&cmdOptions.Debug, "debug", "d",
viper.GetBool(envDebug), "enable verbose or debug logging. Environment Variable: "+envDebug)
rootCmd.PersistentFlags().BoolVarP(&cmdOptions.SkipSsl, "skip-ssl-validation", "k",
viper.GetBool(envSslSkip), "skip validating certificate. Environment Variable: "+envSslSkip)
rootCmd.PersistentFlags().IntVarP(&cmdOptions.Interval, "interval", "i",
viper.GetInt(envInterval), "scrapping interval in seconds. Environment Variable: "+envInterval)
rootCmd.PersistentFlags().IntVarP(&cmdOptions.Port, "port", "p",
viper.GetInt(envPort), "port number to start the web server. Environment Variable: "+envPort)
rootCmd.PersistentFlags().StringVarP(&cmdOptions.OpsManHostname, "opsman-address", "a",
viper.GetString(envOpsManUrl),
"[required] provide the hostname or IP address of the ops manager url. Environment Variable: "+envOpsManUrl)
rootCmd.PersistentFlags().StringVarP(&cmdOptions.OpsManUsername, "opsman-username", "u",
viper.GetString(envOpsManUserName),
"[required if you have setup user using UAAC USER] provide the username to connect to ops manager. Environment Variable: "+envOpsManUserName)
rootCmd.PersistentFlags().StringVarP(&cmdOptions.OpsManPassword, "opsman-password", "w",
viper.GetString(envOpsManPassword),
"[required if you have setup user using UAAC USER] provide the password to connect to ops manager. Environment Variable: "+envOpsManPassword)
rootCmd.PersistentFlags().StringVarP(&cmdOptions.OpsManClientID, "opsman-client-id", "n",
viper.GetString(envOpsManClientID),
"[required if you have setup user using UAAC CLIENT] provide the username to connect to ops manager. Environment Variable: "+envOpsManClientID)
rootCmd.PersistentFlags().StringVarP(&cmdOptions.OpsManClientSecret, "opsman-client-secret", "s",
viper.GetString(envOpsManClientSecret),
"[required if you have setup user using UAAC CLIENT] provide the password to connect to ops manager. Environment Variable: "+envOpsManClientSecret)
rootCmd.PersistentFlags().StringVarP(&cmdOptions.Environment, "environment", "e",
viper.GetString(envEnvironment),
"[required] provide the environment name for this foundation. Environment Variable: "+envEnvironment)
rootCmd.PersistentFlags().StringVarP(&cmdOptions.CACertFile, "ca-cert-file", "c",
viper.GetString(envCaCertFile),
"[required if skip ssl is false] provide the environment name for this foundation. Environment Variable: "+envCaCertFile)
}