-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.go
More file actions
498 lines (417 loc) · 11.3 KB
/
config.go
File metadata and controls
498 lines (417 loc) · 11.3 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package commands
import (
"bufio"
"bytes"
"fmt"
"os"
"regexp"
"runtime"
"strings"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
"github.com/drycc/controller-sdk-go/appsettings"
"github.com/drycc/controller-sdk-go/config"
"github.com/drycc/workflow-cli/internal/utils"
)
// ConfigInfo for an app
func (d *DryccCmd) ConfigInfo(appID string, ptype string, group string, version int) error {
appID, s, err := utils.LoadAppSettings(d.ConfigFile, appID)
if err != nil {
return err
}
config, err := config.List(s.Client, appID, version)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
// init output struct
cv := api.ConfigInfo{
Group: make(map[string][]api.ConfigVar),
Ptype: make(map[string]api.PtypeValue),
}
hasGlobalGroup := false
for _, value := range sortConfigValues(config.Values) {
if value.Group == "global" {
hasGlobalGroup = true
}
// display the selected or all
if (ptype != "" && value.Ptype == ptype) ||
(group != "" && value.Group == group) ||
(ptype == "" && group == "") {
if value.Group != "" {
cv.Group[value.Group] = append(cv.Group[value.Group], api.ConfigVar{Name: value.Name, Value: value.Value})
} else if value.Ptype != "" {
temp := cv.Ptype[value.Ptype]
temp.Env = append(temp.Env, api.ConfigVar{Name: value.Name, Value: value.Value})
cv.Ptype[value.Ptype] = temp
if len(config.ValuesRefs[value.Ptype]) != 0 {
temp.Ref = config.ValuesRefs[value.Ptype]
cv.Ptype[value.Ptype] = temp
}
}
}
}
for k, v := range config.ValuesRefs {
if (ptype != "" && k == ptype) ||
(ptype == "" && group == "") {
if k != "" {
if len(v) != 0 {
temp := cv.Ptype[k]
temp.Ref = v
cv.Ptype[k] = temp
}
}
}
}
if len(cv.Ptype) == 0 && len(cv.Group) == 0 {
d.Println()
return nil
}
// Format and print the output
// print group
for group, configVars := range cv.Group {
d.Println("---\n# Group:", group)
var content string
for _, configVar := range configVars {
content += fmt.Sprintf("%s=%v\n", configVar.Name, configVar.Value)
}
d.Println(content)
}
// print ptype
for ptype, ptypeValue := range cv.Ptype {
d.Printf("---\n# Ptype %s config\n", ptype)
if len(ptypeValue.Env) > 0 {
d.Println("## env")
var content string
for _, configVar := range ptypeValue.Env {
content += fmt.Sprintf("%s=%v\n", configVar.Name, configVar.Value)
}
d.Println(content)
}
if len(ptypeValue.Ref) > 0 {
d.Println("## ref")
var content string
for _, ref := range ptypeValue.Ref {
content += fmt.Sprintf("- %s\n", ref)
}
if hasGlobalGroup {
content += fmt.Sprintf("- %s\n", "global")
}
d.Println(content)
}
}
return nil
}
// ConfigSet sets an app's config variables.
func (d *DryccCmd) ConfigSet(appID string, ptype string, group string, configVars []string, confirm string) error {
appID, s, err := utils.LoadAppSettings(d.ConfigFile, appID)
if err != nil {
return err
}
if ptype == "" && group == "" {
group = "global"
}
err = configConfirmAction(s.Client, appID, ptype, group, confirm)
if err != nil {
return err
}
configMap, err := parseConfig(ptype, group, configVars)
if err != nil {
return err
}
d.Print("Creating config... ")
quit := progress(d.WOut)
configObj := api.Config{Values: configMap}
_, err = config.Set(s.Client, appID, configObj)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Print("done\n\n")
return nil
}
// ConfigUnset removes a config variable from an app.
func (d *DryccCmd) ConfigUnset(appID string, ptype string, group string, configVars []string, confirm string) error {
appID, s, err := utils.LoadAppSettings(d.ConfigFile, appID)
if err != nil {
return err
}
if ptype == "" && group == "" {
group = "global"
}
err = configConfirmAction(s.Client, appID, ptype, group, confirm)
if err != nil {
return err
}
d.Print("Removing config... ")
quit := progress(d.WOut)
valuesMaps := []api.ConfigValue{}
for _, configVar := range configVars {
valuesMap := api.ConfigValue{
Ptype: ptype,
Group: group,
ConfigVar: api.ConfigVar{
Name: configVar,
Value: nil,
},
}
valuesMaps = append(valuesMaps, valuesMap)
}
configObj := api.Config{Values: valuesMaps}
_, err = config.Set(s.Client, appID, configObj)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Print("done\n\n")
return nil
}
// ConfigPull pulls an app's config to a file.
func (d *DryccCmd) ConfigPull(appID, ptype, group, fileName string, interactive bool, overwrite bool) error {
appID, s, err := utils.LoadAppSettings(d.ConfigFile, appID)
if err != nil {
return err
}
if ptype == "" && group == "" {
group = "global"
}
if ptype != "" && group != "" {
d.Println("Only one of ptype and group can be selected.")
return nil
}
configVars, err := config.List(s.Client, appID, -1)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
stat, err := os.Stdout.Stat()
if err != nil {
return err
}
configValues := []api.ConfigValue{}
for _, value := range configVars.Values {
if (ptype != "" && value.Ptype == ptype) ||
(group != "" && value.Group == group) {
configValues = append(configValues, value)
}
}
if (stat.Mode() & os.ModeCharDevice) == 0 {
d.Print(formatConfig(configValues))
return nil
}
if !overwrite {
if _, err := os.Stat(fileName); err == nil {
return fmt.Errorf("%s already exists, pass -o to overwrite", fileName)
}
}
if interactive {
configMap, err := drycc.ParseEnv(fileName)
if err != nil {
return err
}
for _, value := range configValues {
localValue, ok := configMap[value.Name]
if ok {
if value.Value != localValue {
var confirm string
d.Printf("%s: overwrite %s with %s? (y/N) ", value.Name, localValue, value)
fmt.Scanln(&confirm)
if strings.ToLower(confirm) == "y" {
configMap[value.Name] = value.Value
}
}
} else {
configMap[value.Name] = value.Value
}
}
return os.WriteFile(fileName, []byte(formatEnv(configMap)), 0664)
}
return os.WriteFile(fileName, []byte(formatConfig(configValues)), 0664)
}
// ConfigPush pushes an app's config from a file.
func (d *DryccCmd) ConfigPush(appID, ptype string, group string, fileName string, confirm string) error {
stat, err := os.Stdin.Stat()
if err != nil {
return err
}
appID, s, err := utils.LoadAppSettings(d.ConfigFile, appID)
if err != nil {
return err
}
if ptype == "" && group == "" {
group = "global"
}
var contents []byte
if (stat.Mode() & os.ModeCharDevice) == 0 {
err = configConfirmActionStdin(s.Client, appID, ptype, group, confirm)
if err != nil {
return err
}
buffer := new(bytes.Buffer)
buffer.ReadFrom(os.Stdin)
contents = buffer.Bytes()
} else {
err = configConfirmAction(s.Client, appID, ptype, group, confirm)
if err != nil {
return err
}
contents, err = os.ReadFile(fileName)
if err != nil {
return err
}
}
file := strings.Split(string(contents), "\n")
config := []string{}
for _, configVar := range file {
// If file has CRLF encoding, the default on windows, strip the CR
configVar = strings.Trim(configVar, "\r")
if len(configVar) > 0 {
config = append(config, configVar)
}
}
return d.ConfigSet(appID, ptype, group, config, "yes")
}
func (d *DryccCmd) ConfigAttach(appID string, ptype string, groups string) error {
appID, s, err := utils.LoadAppSettings(d.ConfigFile, appID)
if err != nil {
return err
}
d.Print("Attach config... ")
quit := progress(d.WOut)
gs := strings.Split(groups, ",")
refs := api.ValuesRefs{
ptype: gs,
}
configObj := api.Config{ValuesRefs: refs}
_, err = config.Set(s.Client, appID, configObj)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Print("done\n\n")
return nil
}
func (d *DryccCmd) ConfigDetach(appID string, ptype string, groups string) error {
appID, s, err := utils.LoadAppSettings(d.ConfigFile, appID)
if err != nil {
return err
}
d.Print("Detach config... ")
quit := progress(d.WOut)
gs := strings.Split(groups, ",")
refs := api.ValuesRefs{
ptype: gs,
}
configObj := api.Config{ValuesRefs: refs}
err = config.Detach(s.Client, appID, configObj)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Print("done\n\n")
return nil
}
func parseConfig(ptype, group string, configVars []string) ([]api.ConfigValue, error) {
configMap := []api.ConfigValue{}
regex := regexp.MustCompile(`^([A-z0-9_\-\.]+)=([\s\S]*)$`)
for _, config := range configVars {
// Skip config that starts with an comment
if config[0] == '#' {
continue
}
if regex.MatchString(config) {
captures := regex.FindStringSubmatch(config)
value := api.ConfigValue{
Ptype: ptype,
Group: group,
ConfigVar: api.ConfigVar{
Name: captures[1],
Value: captures[2],
},
}
configMap = append(configMap, value)
} else {
return nil, fmt.Errorf("'%s' does not match the pattern 'key=var', ex: MODE=test", config)
}
}
return configMap, nil
}
func formatEnv(configVars map[string]interface{}) string {
var formattedConfig string
keys := *sortKeys(configVars)
for _, key := range keys {
formattedConfig += fmt.Sprintf("%s=%v\n", key, configVars[key])
}
return formattedConfig
}
func formatConfig(configVars []api.ConfigValue) string {
var formattedConfig string
values := sortConfigValues(configVars)
for _, value := range values {
formattedConfig += fmt.Sprintf("%s=%v\n", value.Name, value.Value)
}
return formattedConfig
}
func configConfirmAction(s *drycc.Client, appID string, ptype string, group string, confirm string) error {
if ptype != "" && group != "" {
fmt.Println("Only one of ptype and group can be selected.")
return nil
} else if ptype == "" && group == "" {
group = "global"
}
appSettings, _ := appsettings.List(s, appID)
autodeploy := true
if appSettings.Autodeploy != nil && !*appSettings.Autodeploy {
autodeploy = false
}
if ptype == "" && group == "" && (confirm == "" || confirm != "yes") && autodeploy {
fmt.Printf(` ! WARNING: Potentially Config Action
! This command will deploy all processes of the application
! To proceed, type "yes" !
> `)
fmt.Scanln(&confirm)
if confirm != "yes" {
return fmt.Errorf("cancel the config action")
}
}
return nil
}
func configConfirmActionStdin(s *drycc.Client, appID string, ptype string, group string, confirm string) error {
var reader *bufio.Reader
if runtime.GOOS == "windows" {
reader = bufio.NewReader(os.Stdin)
} else {
file, err := os.Open("/dev/tty")
if err != nil {
return err
}
defer file.Close()
reader = bufio.NewReader(file)
}
if ptype != "" && group != "" {
fmt.Println("Only one of ptype and group can be selected.")
return nil
}
appSettings, _ := appsettings.List(s, appID)
autodeploy := true
if appSettings.Autodeploy != nil && !*appSettings.Autodeploy {
autodeploy = false
}
if ptype == "" && group == "" && (confirm == "" || confirm != "yes") && autodeploy {
fmt.Printf(` ! WARNING: Potentially Config Action
! This command will deploy all processes of the application
! To proceed, type "yes" !
> `)
confirm, err := reader.ReadString('\n')
if err != nil {
return err
}
confirm = strings.TrimSpace(confirm)
if confirm != "yes" {
return fmt.Errorf("cancel the config action")
}
}
return nil
}