From 9dfa58466cff37a2f26d95b24d6b65d31a550f53 Mon Sep 17 00:00:00 2001 From: Doug Schnee Date: Thu, 25 Feb 2021 14:45:29 -0700 Subject: [PATCH 1/2] Adding dry-run/confirm flags to the 'add' command --- cli/add.go | 27 +++++++++++++++++++++++---- doc/commands/add.md | 2 +- test/suites/commands/add.bats | 19 +++++++++++++++---- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/cli/add.go b/cli/add.go index 88ec48a5..8614a171 100644 --- a/cli/add.go +++ b/cli/add.go @@ -3,6 +3,7 @@ package cli import ( "fmt" + "github.com/cnlubo/promptx" "github.com/fishi0x01/vsh/client" "github.com/fishi0x01/vsh/log" ) @@ -17,10 +18,12 @@ type AddCommand struct { // AddCommandArgs provides a struct for go-arg parsing type AddCommandArgs struct { - Key string `arg:"positional,required"` - Value string `arg:"positional,required"` - Path string `arg:"positional,required"` - Force bool `arg:"-f,--force" help:"Overwrite key if exists"` + Key string `arg:"positional,required"` + Value string `arg:"positional,required"` + Path string `arg:"positional,required"` + Force bool `arg:"-f,--force" help:"Overwrite key if exists"` + Confirm bool `arg:"-y,--confirm" help:"Write results without prompt"` + DryRun bool `arg:"-n,--dry-run" help:"Skip writing results without prompt"` } // Description provides detail on what the command does @@ -63,6 +66,9 @@ func (cmd *AddCommand) Parse(args []string) error { if err != nil { return err } + if cmd.args.DryRun == true && cmd.args.Confirm == true { + cmd.args.Confirm = false + } return nil } @@ -97,5 +103,18 @@ func (cmd *AddCommand) addKeyValue(path string, key string, value string) error } data[key] = value secret.SetData(data) + if cmd.args.Confirm == false && cmd.args.DryRun == false { + p := promptx.NewDefaultConfirm("Write changes to Vault?", false) + result, err := p.Run() + if err != nil { + return fmt.Errorf("Error prompting for confirmation") + } + cmd.args.Confirm = result + } + if cmd.args.Confirm == false { + fmt.Println("Skipping write.") + return nil + } + fmt.Println("Writing!") return cmd.client.Write(path, secret) } diff --git a/doc/commands/add.md b/doc/commands/add.md index b2e3c60e..918c4d93 100644 --- a/doc/commands/add.md +++ b/doc/commands/add.md @@ -1,7 +1,7 @@ # add ```text -add [-f|--force] KEY VALUE PATH +add [-f|--force] [-y|--commit] [-n|--dry-run] KEY VALUE PATH ``` Add operation adds or overwrites a single key at path. diff --git a/test/suites/commands/add.bats b/test/suites/commands/add.bats index 669e4cb2..9d0c64cc 100644 --- a/test/suites/commands/add.bats +++ b/test/suites/commands/add.bats @@ -6,12 +6,12 @@ load ../../bin/plugins/bats-assert/load @test "vault-${VAULT_VERSION} ${KV_BACKEND} 'add'" { ####################################### echo "==== case: add value to non existing path ====" - run ${APP_BIN} -c "add test value ${KV_BACKEND}/fake/path" + run ${APP_BIN} -c "add --confirm test value ${KV_BACKEND}/fake/path" assert_failure ####################################### echo "==== case: add key to existing path ====" - run ${APP_BIN} -c "add test value ${KV_BACKEND}/src/a/foo" + run ${APP_BIN} -c "add --confirm test value ${KV_BACKEND}/src/a/foo" assert_success echo "ensure the key was written to destination" @@ -21,17 +21,28 @@ load ../../bin/plugins/bats-assert/load ####################################### echo "==== case: add existing key to existing path ====" - run ${APP_BIN} -c "add value another ${KV_BACKEND}/src/a/foo" + run ${APP_BIN} -c "add --confirm value another ${KV_BACKEND}/src/a/foo" assert_failure assert_output --partial "Key already exists at path: ${KV_BACKEND}/src/a/foo" ####################################### echo "==== case: overwrite existing key to existing path ====" - run ${APP_BIN} -c "add -f value another ${KV_BACKEND}/src/a/foo" + run ${APP_BIN} -c "add --confirm -f value another ${KV_BACKEND}/src/a/foo" assert_success echo "ensure the key was written to destination" run get_vault_value "value" "${KV_BACKEND}/src/a/foo" assert_success assert_output "another" + + ####################################### + echo "==== case: add dryrun, ensure key not added ====" + run ${APP_BIN} -c "add --dry-run -f proposedvalue willnotexist ${KV_BACKEND}/src/a/foo" + assert_success + assert_line "Skipping write." + + echo "ensure the key was NOT written to destination" + run get_vault_value "proposedvalue" "${KV_BACKEND}/src/a/foo" + assert_output --partial "not present in secret" + } From 09f60b13dd9b1c320d1eb449e2c5b8383b5af29f Mon Sep 17 00:00:00 2001 From: Doug Schnee Date: Fri, 26 Feb 2021 10:22:11 -0700 Subject: [PATCH 2/2] updated add.md to correct typo. --- doc/commands/add.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/commands/add.md b/doc/commands/add.md index 918c4d93..58771455 100644 --- a/doc/commands/add.md +++ b/doc/commands/add.md @@ -1,7 +1,7 @@ # add ```text -add [-f|--force] [-y|--commit] [-n|--dry-run] KEY VALUE PATH +add [-f|--force] [-y|--confirm] [-n|--dry-run] KEY VALUE PATH ``` Add operation adds or overwrites a single key at path.