Skip to content

Commit

Permalink
Adding dry-run/confirm flags to the 'add' command (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
dugshnay authored Feb 27, 2021
1 parent 5837431 commit a230fa9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
27 changes: 23 additions & 4 deletions cli/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"fmt"

"github.com/cnlubo/promptx"
"github.com/fishi0x01/vsh/client"
"github.com/fishi0x01/vsh/log"
)
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion doc/commands/add.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# add

```text
add [-f|--force] KEY VALUE PATH
add [-f|--force] [-y|--confirm] [-n|--dry-run] KEY VALUE PATH
```

Add operation adds or overwrites a single key at path.
Expand Down
19 changes: 15 additions & 4 deletions test/suites/commands/add.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

}

0 comments on commit a230fa9

Please sign in to comment.