11package cmd
22
33import (
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
245279func 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+ }
0 commit comments