-
Notifications
You must be signed in to change notification settings - Fork 128
/
command_line.go
265 lines (231 loc) · 8.84 KB
/
command_line.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
package client
import (
"fmt"
"os"
"path/filepath"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"github.com/armadaproject/armada/internal/common/fileutils"
)
// list of config files loaded into viper
// is appended to manually from within LoadCommandlineArgsFromConfigFile
var mergedConfigFiles []string
// path to config file, as given by viper flags
var cfgFile string
// AddArmadaApiConnectionCommandlineArgs adds command-line flags to a cobra command.
// Arguments given via these flags are later used by LoadCommandlineArgsFromConfigFile.
// Hence, apps that use the client package to load config should call this function as part of
// their initialization.
func AddArmadaApiConnectionCommandlineArgs(rootCmd *cobra.Command) {
rootCmd.PersistentFlags().String("armadaUrl", "localhost:50051", "specify armada server url")
err := viper.BindPFlag("armadaUrl", rootCmd.PersistentFlags().Lookup("armadaUrl"))
if err != nil {
panic(err)
}
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.armadactl.yaml)")
}
// LoadCommandlineArgs loads armadactl config
// armadactl-defaults.yaml - From exePath, where exePath is the path to the armadactl executable
// armada config file - From cfgFile, set by the --config CLI flag, or defaulting to $HOME/.armadactl if not set
// These configs are then merged
func LoadCommandlineArgs() error {
return LoadCommandlineArgsFromConfigFile(cfgFile)
}
// LoadCommandlineArgsFromConfigFile loads armadactl config
// armadactl-defaults.yaml - From exePath, where exePath is the path to the armadactl executable
// armada config file - From cfgFile or defaulting to $HOME/.armadactl
// These configs are then merged
func LoadCommandlineArgsFromConfigFile(cfgFile string) error {
// read config at exePath/armadactl-defaults.yaml, where exePath is the path to the armadactl
// executable (if it exists)
exePath, err := os.Executable()
if err != nil {
return fmt.Errorf("[LoadCommandlineArgsFromConfigFile] error finding executable path: %s", err)
}
exeDir := filepath.Dir(exePath)
configPath := filepath.Join(exeDir, "/armadactl-defaults.yaml")
viper.SetConfigFile(configPath)
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
case *os.PathError:
// Config file not found; ignore
default:
return fmt.Errorf("[LoadCommandlineArgsFromConfigFile] error reading config file %s: %s", viper.ConfigFileUsed(), err)
}
} else {
mergedConfigFiles = append(mergedConfigFiles, viper.ConfigFileUsed())
}
// if no cfgFile is provided, use $HOME/.armadactl
if len(cfgFile) > 0 {
exists, err := fileutils.IsFile(cfgFile)
if err != nil {
return fmt.Errorf("[LoadCommandlineArgsFromConfigFile] error checking if %s exists: %s", cfgFile, err)
}
if !exists {
return fmt.Errorf("[LoadCommandlineArgsFromConfigFile] could not find config file %s", cfgFile)
}
viper.SetConfigFile(cfgFile)
} else {
homeDir, err := homedir.Dir()
if err != nil {
return fmt.Errorf("[LoadCommandlineArgsFromConfigFile] error getting home directory: %s", err)
}
viper.AddConfigPath(homeDir)
viper.SetConfigName(".armadactl")
}
// read environment variables
viper.AutomaticEnv()
// merge in new config with those loaded from armadactl-defaults.yaml
// (note the call to viper.MergeInConfig instead of viper.ReadInConfig)
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
case *os.PathError:
// Config file not found; ignore
default:
return fmt.Errorf("[LoadCommandlineArgsFromConfigFile] error reading config file %s: %s", viper.ConfigFileUsed(), err)
}
} else {
mergedConfigFiles = append(mergedConfigFiles, viper.ConfigFileUsed())
}
return nil
}
// ExtractCommandlineArmadaApiConnectionDetails extracts Armada server connection details from the
// config loaded into viper. Hence, this function must be called after loading config into viper,
// e.g., by calling LoadCommandlineArgsFromConfigFile.
func ExtractCommandlineArmadaApiConnectionDetails() *ApiConnectionDetails {
apiConnectionDetails := &ApiConnectionDetails{}
err := viper.Unmarshal(apiConnectionDetails)
if err != nil {
panic(err)
}
return apiConnectionDetails
}
type armadaClientConfig struct {
ApiVersion string
Users map[string]*userConfig
Clusters map[string]*clusterConfig
Contexts map[string]*contextConfig
CurrentContext string
}
type userConfig struct {
Username string
Password string
}
type clusterConfig struct {
Server string
InsecureSkipTlsVerify bool
}
type contextConfig struct {
Cluster string
User string
}
// validate checks that no names are the empty string, that users/clusters references by the
// contexts exists, and that the current context is defined. Other methods on armadaClientConfig
// are themselves responsible for performing any further validation.
func (config *armadaClientConfig) validate() error {
if config == nil {
return fmt.Errorf("[armadaClientConfig.validate] config is nil")
}
if config.Users != nil {
for name, user := range config.Users {
if name == "" {
return fmt.Errorf("[armadaClientConfig.validate] empty user names not allowed")
}
if user == nil {
return fmt.Errorf("[armadaClientConfig.validate] user %s is nil", name)
}
}
}
if config.Clusters != nil {
for name, cluster := range config.Clusters {
if name == "" {
return fmt.Errorf("[armadaClientConfig.validate] empty cluster names not allowed")
}
if cluster == nil {
return fmt.Errorf("[armadaClientConfig.validate] cluster %s is nil", name)
}
}
}
if config.Contexts != nil {
if config.Users == nil {
return fmt.Errorf("[armadaClientConfig.validate] no users provided")
}
if config.Clusters == nil {
return fmt.Errorf("[armadaClientConfig.validate] no clusters provided")
}
for name, context := range config.Contexts {
if name == "" {
return fmt.Errorf("[armadaClientConfig.validate] empty context names not allowed")
}
if context == nil {
return fmt.Errorf("[armadaClientConfig.validate] context %s is nil", name)
}
if _, ok := config.Clusters[context.Cluster]; !ok {
return fmt.Errorf("[armadaClientConfig.validate] could not find cluster %s referenced by context %s", context.Cluster, name)
}
if _, ok := config.Users[context.User]; !ok {
return fmt.Errorf("[armadaClientConfig.validate] could not find user %s referenced by context %s", context.User, name)
}
}
}
if config.CurrentContext != "" {
if config.Clusters == nil {
return fmt.Errorf("[armadaClientConfig.validate] could not find current context %s", config.CurrentContext)
}
if _, ok := config.Contexts[config.CurrentContext]; !ok {
return fmt.Errorf("[armadaClientConfig.validate] could not find current context %s", config.CurrentContext)
}
}
return nil
}
// ConfigYamlString returns a string representation of the merged configuration stored in viper.
func (config *armadaClientConfig) YamlString() (string, error) {
bs, err := yaml.Marshal(config)
if err != nil {
return "", fmt.Errorf("[armadaClientConfig.YamlString] unable to marshal config to yaml: %s", err)
}
return string(bs), nil
}
// LoadClientConfig reads in config from the file at configPath, or, if configPath is the empty
// string, from $HOME/.armada/config, and from environment variables, using viper.
func LoadClientConfig(configPath string) (*armadaClientConfig, error) {
// if no file is provided (i.e., if configPath is the empty string), default to $HOME/.armada/config
if len(configPath) == 0 {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("[LoadClientConfig] error getting user home directory: %s", err)
}
configPath = filepath.Join(homeDir, "/.armada/", "config")
}
// read config from file; ignore if it doesn't exist
viper.SetConfigType("yaml")
viper.SetConfigFile(configPath)
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore
} else {
return nil, fmt.Errorf("[LoadClientConfig] error reading config file %s: %s", viper.ConfigFileUsed(), err)
}
}
// read environment variables
viper.AutomaticEnv()
// read config into an armadaConfig struct
// var config armadaClientConfig
config := &armadaClientConfig{}
if err := viper.Unmarshal(config); err != nil {
return nil, err
}
if err := config.validate(); err != nil {
return nil, fmt.Errorf("[LoadClientConfig] error validating config: %s", err)
}
return config, nil
}
// CurrentApiConnectionDetails returns connection details for the currently active cluster server,
// as specified by the currentContext setting.
func (config *armadaClientConfig) CurrentApiConnectionDetails() (*ApiConnectionDetails, error) {
return nil, fmt.Errorf("[armadaClientConfig.CurrentApiConnectionDetails] error: not implemented")
}