From 7142f084e7866663b519b4078bf22a44c1103515 Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Sat, 23 Oct 2021 11:29:48 -0700 Subject: [PATCH 1/3] Support supplying `doppler configure set` value on stdin --- pkg/cmd/configure.go | 36 ++++++++++++++++++++++++++++++++++-- pkg/cmd/secrets.go | 8 +++----- pkg/utils/io.go | 12 ++++++++++++ tests/e2e/configure.sh | 7 +++++++ 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/pkg/cmd/configure.go b/pkg/cmd/configure.go index 397df055..961e64ce 100644 --- a/pkg/cmd/configure.go +++ b/pkg/cmd/configure.go @@ -16,8 +16,10 @@ limitations under the License. package cmd import ( + "bufio" "errors" "fmt" + "os" "strings" "github.com/DopplerHQ/cli/pkg/configuration" @@ -127,7 +129,16 @@ doppler configure set key=123 otherkey=456`, } if !strings.Contains(args[0], "=") { - if len(args) == 2 { + if len(args) == 1 { + hasData, e := utils.HasDataOnStdIn() + if e != nil { + utils.HandleError(e) + } + if !hasData { + return errors.New("Value must be suppied on stdin or as an argument") + } + return nil + } else if len(args) == 2 { if configuration.IsValidConfigOption(args[0]) || configuration.IsTranslatableConfigOption(args[0]) { return nil } @@ -150,7 +161,28 @@ doppler configure set key=123 otherkey=456`, jsonFlag := utils.OutputJSON options := map[string]string{} - if !strings.Contains(args[0], "=") { + + if len(args) == 1 && !strings.Contains(args[0], "=") { + var input []string + scanner := bufio.NewScanner(os.Stdin) + // read input from stdin + for { + if ok := scanner.Scan(); !ok { + if e := scanner.Err(); e != nil { + utils.HandleError(e, "Unable to read input from stdin") + } + + break + } + + s := scanner.Text() + input = append(input, s) + } + + key := args[0] + value := strings.Join(input, "\n") + options[key] = value + } else if !strings.Contains(args[0], "=") { options[args[0]] = args[1] } else { for _, option := range args { diff --git a/pkg/cmd/secrets.go b/pkg/cmd/secrets.go index 209eaf23..d805b3f4 100644 --- a/pkg/cmd/secrets.go +++ b/pkg/cmd/secrets.go @@ -237,13 +237,11 @@ func setSecrets(cmd *cobra.Command, args []string) { // format: 'doppler secrets set KEY' (interactive) // check for existing data on stdin - stat, e := os.Stdin.Stat() + hasData, e := utils.HasDataOnStdIn() if e != nil { - utils.HandleError(e, "Unable to stat stdin") + utils.HandleError(e) } - - dataOnStdin := (stat.Mode() & os.ModeCharDevice) == 0 - interactiveMode := !dataOnStdin + interactiveMode := !hasData if interactiveMode { if !canPromptUser { utils.HandleError(errors.New("Secret value must be provided when using --no-interactive")) diff --git a/pkg/utils/io.go b/pkg/utils/io.go index b283135b..2b272241 100644 --- a/pkg/utils/io.go +++ b/pkg/utils/io.go @@ -16,6 +16,7 @@ limitations under the License. package utils import ( + "errors" "fmt" "io/ioutil" "os" @@ -76,3 +77,14 @@ func WriteTempFile(name string, data []byte, perm os.FileMode) (string, error) { return tmpFileName, nil } + +func HasDataOnStdIn() (bool, error) { + stat, e := os.Stdin.Stat() + if e != nil { + LogDebugError(e) + return false, errors.New("Unable to stat stdin") + } + + hasData := (stat.Mode() & os.ModeCharDevice) == 0 + return hasData, nil +} diff --git a/tests/e2e/configure.sh b/tests/e2e/configure.sh index 22368f37..5dd75410 100755 --- a/tests/e2e/configure.sh +++ b/tests/e2e/configure.sh @@ -101,6 +101,13 @@ config="$("$DOPPLER_BINARY" configure get config --configuration=./temp-config - beforeEach +# test set using stdin +echo 123 | "$DOPPLER_BINARY" configure set config --configuration=./temp-config --scope=/ --silent +config="$("$DOPPLER_BINARY" configure get config --configuration=./temp-config --scope=/ --json)" +[[ "$config" == '{"enclave.config":"123"}' ]] || error "ERROR: unexpected config contents after 'set' w/ stdin" + +beforeEach + # # CLI v3 compatability tests (DPLR-435) # From e3ad9f67bddf2e4c62901d18f3ea367a7c0c6d12 Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Sat, 23 Oct 2021 11:30:06 -0700 Subject: [PATCH 2/3] chore: adjust error message --- pkg/cmd/configure.go | 6 +++--- pkg/cmd/run.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/configure.go b/pkg/cmd/configure.go index 961e64ce..eb605507 100644 --- a/pkg/cmd/configure.go +++ b/pkg/cmd/configure.go @@ -88,7 +88,7 @@ doppler configure get key otherkey`, ValidArgsFunction: currentConfigOptionsValidArgs, Args: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - return errors.New("requires at least 1 arg(s), only received 0") + return errors.New("requires at least 1 arg(s), received 0") } for _, arg := range args { @@ -125,7 +125,7 @@ doppler configure set key=123 otherkey=456`, ValidArgsFunction: configOptionsValidArgs, Args: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - return errors.New("requires at least 1 arg(s), only received 0") + return errors.New("requires at least 1 arg(s), received 0") } if !strings.Contains(args[0], "=") { @@ -215,7 +215,7 @@ doppler configure unset key otherkey`, ValidArgsFunction: currentConfigOptionsValidArgs, Args: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - return errors.New("requires at least 1 arg(s), only received 0") + return errors.New("requires at least 1 arg(s), received 0") } for _, arg := range args { diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 12e0a602..8016a9a5 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -61,7 +61,7 @@ doppler run --command "YOUR_COMMAND && YOUR_OTHER_COMMAND"`, return errors.New("arg(s) may not be set when using --command flag") } } else if len(args) == 0 { - return errors.New("requires at least 1 arg(s), only received 0") + return errors.New("requires at least 1 arg(s), received 0") } return nil From 07d368f54df603dbc296ccd178614882375bee7d Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Sat, 23 Oct 2021 12:05:51 -0700 Subject: [PATCH 3/3] chore: better error message when value not supplied --- pkg/cmd/configure.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/configure.go b/pkg/cmd/configure.go index eb605507..fc6f3cf9 100644 --- a/pkg/cmd/configure.go +++ b/pkg/cmd/configure.go @@ -150,9 +150,12 @@ doppler configure set key=123 otherkey=456`, for _, arg := range args { option := strings.Split(arg, "=") - if len(option) < 2 || (!configuration.IsValidConfigOption(option[0]) && !configuration.IsTranslatableConfigOption(option[0])) { + if !configuration.IsValidConfigOption(option[0]) && !configuration.IsTranslatableConfigOption(option[0]) { return errors.New("invalid option " + option[0]) } + if len(option) < 2 { + return errors.New("option " + option[0] + " requires a value") + } } return nil