/
config.go
398 lines (372 loc) · 11.5 KB
/
config.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
package cmd
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"github.com/amazeeio/lagoon-cli/internal/lagoon"
"github.com/amazeeio/lagoon-cli/internal/lagoon/client"
"github.com/amazeeio/lagoon-cli/pkg/output"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v2"
)
// LagoonConfigFlags .
type LagoonConfigFlags struct {
Lagoon string `json:"lagoon,omitempty"`
Hostname string `json:"hostname,omitempty"`
Port string `json:"port,omitempty"`
GraphQL string `json:"graphql,omitempty"`
Token string `json:"token,omitempty"`
UI string `json:"ui,omitempty"`
Kibana string `json:"kibana,omitempty"`
}
func parseLagoonConfig(flags pflag.FlagSet) LagoonConfigFlags {
configMap := make(map[string]interface{})
flags.VisitAll(func(f *pflag.Flag) {
if flags.Changed(f.Name) {
configMap[f.Name] = f.Value
}
})
jsonStr, _ := json.Marshal(configMap)
parsedFlags := LagoonConfigFlags{}
json.Unmarshal(jsonStr, &parsedFlags)
return parsedFlags
}
var configCmd = &cobra.Command{
Use: "config",
Aliases: []string{"c"},
Short: "Configure Lagoon CLI",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
},
}
var configDefaultCmd = &cobra.Command{
Use: "default",
Aliases: []string{"d"},
Short: "Set the default Lagoon to use",
Run: func(cmd *cobra.Command, args []string) {
lagoonConfig := parseLagoonConfig(*cmd.Flags())
if lagoonConfig.Lagoon == "" {
fmt.Println("Not enough arguments")
cmd.Help()
os.Exit(1)
}
lagoonCLIConfig.Default = strings.TrimSpace(string(lagoonConfig.Lagoon))
contextExists := false
for l := range lagoonCLIConfig.Lagoons {
if l == lagoonCLIConfig.Current {
contextExists = true
}
}
if !contextExists {
fmt.Println(fmt.Printf("Chosen context '%s' doesn't exist in config file", lagoonCLIConfig.Current))
os.Exit(1)
}
err := writeLagoonConfig(&lagoonCLIConfig, filepath.Join(configFilePath, configName+configExtension))
handleError(err)
resultData := output.Result{
Result: "success",
ResultData: map[string]interface{}{
"default-lagoon": lagoonConfig.Lagoon,
},
}
output.RenderResult(resultData, outputOptions)
},
}
var configLagoonsCmd = &cobra.Command{
Use: "list",
Aliases: []string{"l"},
Short: "View all configured Lagoon instances",
RunE: func(cmd *cobra.Command, args []string) error {
var data []output.Data
for l, lc := range lagoonCLIConfig.Lagoons {
var isDefault, isCurrent string
if l == lagoonCLIConfig.Default {
isDefault = "(default)"
}
if l == lagoonCLIConfig.Current {
isCurrent = "(current)"
}
mapData := []string{
returnNonEmptyString(fmt.Sprintf("%s%s%s", l, isDefault, isCurrent)),
returnNonEmptyString(lc.Version),
returnNonEmptyString(lc.GraphQL),
returnNonEmptyString(lc.HostName),
returnNonEmptyString(lc.Port),
returnNonEmptyString(lc.UI),
returnNonEmptyString(lc.Kibana),
}
data = append(data, mapData)
}
sort.Slice(data, func(i, j int) bool {
return data[i][0] < data[j][0]
})
output.RenderOutput(output.Table{
Header: []string{
"Name",
"Version",
"GraphQL",
"SSH-Hostname",
"SSH-Port",
"UI-URL",
"Kibana-URL",
},
Data: data,
}, outputOptions)
return nil
},
}
var configAddCmd = &cobra.Command{
Use: "add",
Aliases: []string{"a"},
Short: "Add information about an additional Lagoon instance to use",
RunE: func(cmd *cobra.Command, args []string) error {
lagoonConfig := parseLagoonConfig(*cmd.Flags())
if lagoonConfig.Lagoon == "" {
return fmt.Errorf("Missing arguments: Lagoon name is not defined")
}
if lagoonConfig.Hostname != "" && lagoonConfig.Port != "" && lagoonConfig.GraphQL != "" {
lc := lagoonCLIConfig.Lagoons[lagoonConfig.Lagoon]
lc.HostName = lagoonConfig.Hostname
lc.Port = lagoonConfig.Port
lc.GraphQL = lagoonConfig.GraphQL
if lagoonConfig.UI != "" {
lc.UI = lagoonConfig.UI
}
if lagoonConfig.Kibana != "" {
lc.Kibana = lagoonConfig.Kibana
}
if lagoonConfig.Token != "" {
lc.Token = lagoonConfig.Token
}
lagoonCLIConfig.Lagoons[lagoonConfig.Lagoon] = lc
if err := writeLagoonConfig(&lagoonCLIConfig, filepath.Join(configFilePath, configName+configExtension)); err != nil {
return fmt.Errorf("couldn't write config: %v", err)
}
output.RenderOutput(output.Table{
Header: []string{
"Name",
"GraphQL",
"SSH-Hostname",
"SSH-Port",
"UI-URL",
"Kibana-URL",
},
Data: []output.Data{
[]string{
lagoonConfig.Lagoon,
lagoonConfig.GraphQL,
lagoonConfig.Hostname,
lagoonConfig.Port,
lagoonConfig.UI,
lagoonConfig.Kibana,
},
},
}, outputOptions)
} else {
return fmt.Errorf("Must have Hostname, Port, and GraphQL endpoint")
}
return nil
},
}
var configDeleteCmd = &cobra.Command{
Use: "delete",
Aliases: []string{"d"},
Short: "Delete a Lagoon instance configuration",
Run: func(cmd *cobra.Command, args []string) {
lagoonConfig := parseLagoonConfig(*cmd.Flags())
if lagoonConfig.Lagoon == "" {
fmt.Println("Missing arguments: Lagoon name is not defined")
cmd.Help()
os.Exit(1)
}
if yesNo(fmt.Sprintf("You are attempting to delete config for lagoon '%s', are you sure?", lagoonConfig.Lagoon)) {
err := removeConfig(lagoonConfig.Lagoon)
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
}
}
},
}
var configFeatureSwitch = &cobra.Command{
Use: "feature",
Aliases: []string{"f"},
Short: "Enable or disable CLI features",
Run: func(cmd *cobra.Command, args []string) {
switch updateCheck {
case "true":
lagoonCLIConfig.UpdateCheckDisable = true
case "false":
lagoonCLIConfig.UpdateCheckDisable = false
}
switch environmentFromDirectory {
case "true":
lagoonCLIConfig.EnvironmentFromDirectory = true
case "false":
lagoonCLIConfig.EnvironmentFromDirectory = false
}
if err := writeLagoonConfig(&lagoonCLIConfig, filepath.Join(configFilePath, configName+configExtension)); err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
}
},
}
var configGetCurrent = &cobra.Command{
Use: "current",
Aliases: []string{"cur"},
Short: "Display the current Lagoon that commands would be executed against",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(lagoonCLIConfig.Current)
},
}
var configLagoonVersionCmd = &cobra.Command{
Use: "lagoon-version",
Aliases: []string{"l"},
Short: "Checks the current Lagoon for its version and sets it in the config file",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
current := lagoonCLIConfig.Current
lc := client.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
debug)
lagoonVersion, err := lagoon.GetLagoonAPIVersion(context.TODO(), lc)
if err != nil {
return err
}
lagu := lagoonCLIConfig.Lagoons[current]
lagu.Version = lagoonVersion.LagoonVersion
lagoonCLIConfig.Lagoons[current] = lagu
if err = writeLagoonConfig(&lagoonCLIConfig, filepath.Join(configFilePath, configName+configExtension)); err != nil {
return fmt.Errorf("couldn't write config: %v", err)
}
fmt.Println(lagoonVersion.LagoonVersion)
return nil
},
}
var updateCheck string
var environmentFromDirectory string
func init() {
configCmd.AddCommand(configAddCmd)
configCmd.AddCommand(configGetCurrent)
configCmd.AddCommand(configDefaultCmd)
configCmd.AddCommand(configDeleteCmd)
configCmd.AddCommand(configFeatureSwitch)
configCmd.AddCommand(configLagoonsCmd)
configCmd.AddCommand(configLagoonVersionCmd)
configAddCmd.Flags().StringVarP(&lagoonHostname, "hostname", "H", "",
"Lagoon SSH hostname")
configAddCmd.Flags().StringVarP(&lagoonPort, "port", "P", "",
"Lagoon SSH port")
configAddCmd.Flags().StringVarP(&lagoonGraphQL, "graphql", "g", "",
"Lagoon GraphQL endpoint")
configAddCmd.Flags().StringVarP(&lagoonToken, "token", "t", "",
"Lagoon GraphQL token")
configAddCmd.Flags().StringVarP(&lagoonUI, "ui", "u", "",
"Lagoon UI location (https://dashboard.amazeeio.cloud)")
configAddCmd.PersistentFlags().BoolVarP(&createConfig, "create-config", "", false,
"Create the config file if it is non existent (to be used with --config-file)")
configAddCmd.Flags().StringVarP(&lagoonKibana, "kibana", "k", "",
"Lagoon Kibana URL (https://logs.amazeeio.cloud)")
configFeatureSwitch.Flags().StringVarP(&updateCheck, "disable-update-check", "", "",
"Enable or disable checking of updates (true/false)")
configFeatureSwitch.Flags().StringVarP(&environmentFromDirectory, "enable-local-dir-check", "", "",
"Enable or disable checking of local directory for Lagoon project (true/false)")
}
// readLagoonConfig reads the lagoon config from specified file.
func readLagoonConfig(lc *lagoon.Config, file string) error {
data, err := ioutil.ReadFile(file)
if err != nil {
// if there is no file found in the specified location, prompt the user to create it with the default
// configuration to point to the amazeeio lagoon instance
if yesNo(fmt.Sprintf("Config file '%s' does not exist, do you want to create it with defaults?", file)) {
l := lagoon.Context{
GraphQL: "https://api.lagoon.amazeeio.cloud/graphql",
HostName: "ssh.lagoon.amazeeio.cloud",
Token: "",
Port: "32222",
UI: "https://dashboard.amazeeio.cloud",
Kibana: "https://logs.amazeeio.cloud/",
}
lc.Lagoons = map[string]lagoon.Context{}
lc.Lagoons["amazeeio"] = l
lc.Default = "amazeeio"
return writeLagoonConfig(lc, file)
}
return err
}
err = yaml.Unmarshal(data, &lc)
if err != nil {
return fmt.Errorf("unable to unmarshal config, yaml is likely invalid: %v", err)
}
for ln, l := range lc.Lagoons {
if l.GraphQL == "" || l.HostName == "" || l.Port == "" {
return fmt.Errorf("configured lagoon %s is missing required configuration for graphql, hostname, or port", ln)
}
}
return nil
}
func analyze(file string) error {
handle, err := os.Open(file)
if err != nil {
return err
}
defer handle.Close()
return doSomething(handle)
}
func doSomething(handle io.Reader) error {
scanner := bufio.NewScanner(handle)
for scanner.Scan() {
// Do something with line
d := scanner.Text()
fmt.Println(d)
}
return nil
}
// functions to handle read/write of configuration file
// writeLagoonConfig writes the lagoon config to specified file.
func writeLagoonConfig(lc *lagoon.Config, file string) error {
d, err := yaml.Marshal(&lc)
if err != nil {
return fmt.Errorf("unable to marshal config into valid yaml: %v", err)
}
err = ioutil.WriteFile(file, d, 0777)
if err != nil {
return err
}
return nil
}
func setConfigDefaultVersion(lc *lagoon.Config, lagoon string, version string) error {
if lc.Lagoons[lagoon].Version == "" {
l := lc.Lagoons[lagoon]
l.Version = version
lc.Lagoons[lagoon] = l
if err := writeLagoonConfig(&lagoonCLIConfig, filepath.Join(configFilePath, configName+configExtension)); err != nil {
return fmt.Errorf("couldn't write config: %v", err)
}
}
return nil
}
func removeConfig(key string) error {
delete(lagoonCLIConfig.Lagoons, key)
if err := writeLagoonConfig(&lagoonCLIConfig, filepath.Join(configFilePath, configName+configExtension)); err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
}
return nil
}