Skip to content

Commit 2645bb2

Browse files
committed
chore(workflow-cli): config action add confirm
chore(workflow-cli$): config action add confirm
1 parent 782a301 commit 2645bb2

6 files changed

Lines changed: 123 additions & 25 deletions

File tree

cmd/cmd.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ type Commander interface {
4242
CertAttach(string, string) error
4343
CertDetach(string, string) error
4444
ConfigList(string, string) error
45-
ConfigSet(string, string, []string) error
46-
ConfigUnset(string, string, []string) error
45+
ConfigSet(string, string, []string, string) error
46+
ConfigUnset(string, string, []string, string) error
4747
ConfigPull(string, string, string, bool, bool) error
48-
ConfigPush(string, string, string) error
48+
ConfigPush(string, string, string, string) error
4949
DomainsList(string, int) error
5050
DomainsAdd(string, string, string) error
5151
DomainsRemove(string, string) error

cmd/config.go

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package cmd
22

33
import (
4+
"bufio"
45
"bytes"
56
"encoding/base64"
67
"fmt"
78
"os"
89
"regexp"
10+
"runtime"
911
"strings"
1012

1113
"github.com/drycc/controller-sdk-go/api"
@@ -37,18 +39,36 @@ func (d *DryccCmd) ConfigList(appID string, procType string) error {
3739
fmt.Sprintf("%v", configValues[key]),
3840
})
3941
}
42+
43+
// common config
44+
if procType != "" {
45+
table.Append([]string{"--- Common Config:"})
46+
configValues = config.Values
47+
keys = *sortKeys(configValues)
48+
for _, key := range keys {
49+
table.Append([]string{
50+
key,
51+
fmt.Sprintf("%v", configValues[key]),
52+
})
53+
}
54+
}
4055
table.Render()
4156
return nil
4257
}
4358

4459
// ConfigSet sets an app's config variables.
45-
func (d *DryccCmd) ConfigSet(appID string, procType string, configVars []string) error {
60+
func (d *DryccCmd) ConfigSet(appID string, procType string, configVars []string, confirm string) error {
4661
s, appID, err := load(d.ConfigFile, appID)
4762

4863
if err != nil {
4964
return err
5065
}
5166

67+
err = configConfirmAction(procType, confirm)
68+
if err != nil {
69+
return err
70+
}
71+
5272
configMap, err := parseConfig(configVars)
5373
if err != nil {
5474
return err
@@ -97,13 +117,18 @@ to set up healthchecks. This functionality has been deprecated. In the future, p
97117
}
98118

99119
// ConfigUnset removes a config variable from an app.
100-
func (d *DryccCmd) ConfigUnset(appID string, procType string, configVars []string) error {
120+
func (d *DryccCmd) ConfigUnset(appID string, procType string, configVars []string, confirm string) error {
101121
s, appID, err := load(d.ConfigFile, appID)
102122

103123
if err != nil {
104124
return err
105125
}
106126

127+
err = configConfirmAction(procType, confirm)
128+
if err != nil {
129+
return err
130+
}
131+
107132
d.Print("Removing config... ")
108133

109134
quit := progress(d.WOut)
@@ -207,7 +232,7 @@ func (d *DryccCmd) ConfigPull(appID, procType, fileName string, interactive bool
207232
}
208233

209234
// ConfigPush pushes an app's config from a file.
210-
func (d *DryccCmd) ConfigPush(appID, procType string, fileName string) error {
235+
func (d *DryccCmd) ConfigPush(appID, procType string, fileName string, confirm string) error {
211236
stat, err := os.Stdin.Stat()
212237

213238
if err != nil {
@@ -217,10 +242,19 @@ func (d *DryccCmd) ConfigPush(appID, procType string, fileName string) error {
217242
var contents []byte
218243

219244
if (stat.Mode() & os.ModeCharDevice) == 0 {
245+
err = configConfirmActionStdin(procType, confirm)
246+
if err != nil {
247+
return err
248+
}
220249
buffer := new(bytes.Buffer)
221250
buffer.ReadFrom(os.Stdin)
222251
contents = buffer.Bytes()
223252
} else {
253+
err = configConfirmAction(procType, confirm)
254+
if err != nil {
255+
return err
256+
}
257+
224258
contents, err = os.ReadFile(fileName)
225259

226260
if err != nil {
@@ -239,7 +273,7 @@ func (d *DryccCmd) ConfigPush(appID, procType string, fileName string) error {
239273
}
240274
}
241275

242-
return d.ConfigSet(appID, procType, config)
276+
return d.ConfigSet(appID, procType, config, "yes")
243277
}
244278

245279
func parseConfig(configVars []string) (api.ConfigValues, error) {
@@ -304,3 +338,52 @@ func formatConfig(configVars map[string]interface{}) string {
304338

305339
return formattedConfig
306340
}
341+
342+
func configConfirmAction(procType string, confirm string) error {
343+
if procType == "" && (confirm == "" || confirm != "yes") {
344+
fmt.Printf(` ! WARNING: Potentially Config Action
345+
! This command will deploy all processes of the application
346+
! To proceed, type "yes" !
347+
348+
> `)
349+
350+
fmt.Scanln(&confirm)
351+
if confirm != "yes" {
352+
return fmt.Errorf("cancel the config action")
353+
}
354+
}
355+
return nil
356+
}
357+
358+
func configConfirmActionStdin(procType string, confirm string) error {
359+
var reader *bufio.Reader
360+
if runtime.GOOS == "windows" {
361+
reader = bufio.NewReader(os.Stdin)
362+
} else {
363+
file, err := os.Open("/dev/tty")
364+
if err != nil {
365+
return err
366+
}
367+
defer file.Close()
368+
reader = bufio.NewReader(file)
369+
}
370+
371+
if procType == "" && (confirm == "" || confirm != "yes") {
372+
fmt.Printf(` ! WARNING: Potentially Config Action
373+
! This command will deploy all processes of the application
374+
! To proceed, type "yes" !
375+
376+
> `)
377+
378+
confirm, err := reader.ReadString('\n')
379+
if err != nil {
380+
return err
381+
}
382+
383+
confirm = strings.TrimSpace(confirm)
384+
if confirm != "yes" {
385+
return fmt.Errorf("cancel the config action")
386+
}
387+
}
388+
return nil
389+
}

cmd/config_test.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,13 @@ TRUE false
136136
b.Reset()
137137
err = cmdr.ConfigList("foo", "web")
138138
assert.NoError(t, err)
139-
assert.Equal(t, b.String(), `NAME VALUE
140-
PORT 9000
139+
assert.Equal(t, b.String(), `NAME VALUE
140+
PORT 9000
141+
--- Common Config:
142+
FLOAT 12.34
143+
NCC 1701
144+
TEST testing
145+
TRUE false
141146
`, "output")
142147

143148
}
@@ -184,7 +189,7 @@ func TestConfigSet(t *testing.T) {
184189
var b bytes.Buffer
185190
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
186191

187-
err = cmdr.ConfigSet("foo", "", []string{"TRUE=false", "SSH_KEY=-----BEGIN OPENSSH PRIVATE KEY-----"})
192+
err = cmdr.ConfigSet("foo", "", []string{"TRUE=false", "SSH_KEY=-----BEGIN OPENSSH PRIVATE KEY-----"}, "yes")
188193
assert.NoError(t, err)
189194

190195
assert.Equal(t, testutil.StripProgress(b.String()), `Creating config... done
@@ -238,7 +243,7 @@ func TestConfigUnset(t *testing.T) {
238243
var b bytes.Buffer
239244
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
240245

241-
err = cmdr.ConfigUnset("foo", "", []string{"FOO"})
246+
err = cmdr.ConfigUnset("foo", "", []string{"FOO"}, "yes")
242247
assert.NoError(t, err)
243248

244249
assert.Equal(t, testutil.StripProgress(b.String()), `Removing config... done
@@ -298,15 +303,17 @@ func TestConfigUnsetTypedValues(t *testing.T) {
298303
var b bytes.Buffer
299304
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
300305

301-
err = cmdr.ConfigUnset("foo", "web", []string{"FOO"})
306+
err = cmdr.ConfigUnset("foo", "web", []string{"FOO"}, "")
302307
assert.NoError(t, err)
303308

304309
assert.Equal(t, testutil.StripProgress(b.String()), `Removing config... done
305310
306-
NAME VALUE
307-
FLOAT 12.34
308-
NCC 1701
309-
TEST testing
310-
TRUE false
311+
NAME VALUE
312+
FLOAT 12.34
313+
NCC 1701
314+
TEST testing
315+
TRUE false
316+
--- Common Config:
317+
RELEASE_VERSION v1
311318
`, "output")
312319
}

cmd/ps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (d *DryccCmd) PsRestart(appID string, targets []string, confirm string) err
143143
if err != nil {
144144
return err
145145
}
146-
if len(targets) == 0 && confirm == "" {
146+
if len(targets) == 0 && (confirm == "" || confirm != "yes") {
147147
d.Printf(` ! WARNING: Potentially Restart Action
148148
! This command will restart all processes of the application
149149
! To proceed, type "yes" !

parser/config.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ Options:
8585
the application that you wish to set.
8686
--type=<type>
8787
the procType for which the config needs to be set.
88+
--confirm=yes
89+
To proceed, type "yes".
8890
`
8991

9092
args, err := docopt.ParseArgs(usage, argv, "")
@@ -95,8 +97,8 @@ Options:
9597

9698
app := safeGetString(args, "--app")
9799
procType := safeGetString(args, "--type")
98-
99-
return cmdr.ConfigSet(app, procType, args["<var>=<value>"].([]string))
100+
confirm := safeGetString(args, "--confirm")
101+
return cmdr.ConfigSet(app, procType, args["<var>=<value>"].([]string), confirm)
100102
}
101103

102104
func configUnset(argv []string, cmdr cmd.Commander) error {
@@ -114,6 +116,8 @@ Options:
114116
the application that you wish to unset.
115117
--type=<type>
116118
the procType for which the config needs to be unset.
119+
--confirm=yes
120+
To proceed, type "yes".
117121
`
118122

119123
args, err := docopt.ParseArgs(usage, argv, "")
@@ -123,7 +127,8 @@ Options:
123127
}
124128
app := safeGetString(args, "--app")
125129
procType := safeGetString(args, "--type")
126-
return cmdr.ConfigUnset(app, procType, args["<key>"].([]string))
130+
confirm := safeGetString(args, "--confirm")
131+
return cmdr.ConfigUnset(app, procType, args["<key>"].([]string), confirm)
127132
}
128133

129134
func configPull(argv []string, cmdr cmd.Commander) error {
@@ -181,6 +186,8 @@ Options:
181186
the procType for which the config needs to be push.
182187
--path=<path>
183188
a path leading to an environment file [default: .env]
189+
--confirm=yes
190+
To proceed, type "yes".
184191
`
185192

186193
args, err := docopt.ParseArgs(usage, argv, "")
@@ -192,5 +199,6 @@ Options:
192199
app := safeGetString(args, "--app")
193200
procType := safeGetString(args, "--type")
194201
path := safeGetValue(args, "--path", ".env")
195-
return cmdr.ConfigPush(app, procType, path)
202+
confirm := safeGetString(args, "--confirm")
203+
return cmdr.ConfigPush(app, procType, path, confirm)
196204
}

parser/config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ func (d FakeDryccCmd) ConfigList(string, string) error {
1616
return errors.New("config:list")
1717
}
1818

19-
func (d FakeDryccCmd) ConfigSet(string, string, []string) error {
19+
func (d FakeDryccCmd) ConfigSet(string, string, []string, string) error {
2020
return errors.New("config:set")
2121
}
2222

23-
func (d FakeDryccCmd) ConfigUnset(string, string, []string) error {
23+
func (d FakeDryccCmd) ConfigUnset(string, string, []string, string) error {
2424
return errors.New("config:unset")
2525
}
2626

2727
func (d FakeDryccCmd) ConfigPull(string, string, string, bool, bool) error {
2828
return errors.New("config:pull")
2929
}
3030

31-
func (d FakeDryccCmd) ConfigPush(string, string, string) error {
31+
func (d FakeDryccCmd) ConfigPush(string, string, string, string) error {
3232
return errors.New("config:push")
3333
}
3434

0 commit comments

Comments
 (0)