From e40a71c9e1e85171198bed224c1401e394a66f17 Mon Sep 17 00:00:00 2001 From: Karl Fischer Date: Sat, 10 Apr 2021 16:47:59 +0200 Subject: [PATCH] Default path is pwd for grep and replace (#95) --- CHANGELOG.md | 4 ++++ README.md | 2 +- cli/grep.go | 5 ++++- cli/replace.go | 5 ++++- test/run-all-tests.sh | 2 +- test/run-single-test.sh | 2 +- test/suites/commands/grep.bats | 7 +++++++ test/suites/commands/replace.bats | 10 ++++++++++ 8 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45908ba7..0e4a052d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master - unreleased +ENHANCEMENTS: + +* In interactive mode, assume path for command is pwd for grep and replace ([#95](https://github.com/fishi0x01/vsh/pull/95)) + ## v0.11.0 (February 27, 2021) ENHANCEMENTS: diff --git a/README.md b/README.md index a3cedc51..71e0ab72 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ Working on vault secrets can be critical, making quality and correct behavior a That being said, `vsh` is still a small open source project, meaning we cannot give any guarantees. However, we put strong emphasis on test-driven development. Every PR is tested with an extensive [suite](test/suites) of integration tests. -Vast majority of tests run on KV1 and KV2 and every test runs against vault `1.0.0` and `1.6.2`, i.e., vault versions in between should also be compatible. +Vast majority of tests run on KV1 and KV2 and every test runs against vault `1.0.0` and `1.6.3`, i.e., vault versions in between should also be compatible. ## Contributions diff --git a/cli/grep.go b/cli/grep.go index 2ce7a483..0edfb7ea 100644 --- a/cli/grep.go +++ b/cli/grep.go @@ -21,7 +21,7 @@ type GrepCommand struct { // GrepCommandArgs provides a struct for go-arg parsing type GrepCommandArgs struct { Search string `arg:"positional,required"` - Path string `arg:"positional,required"` + Path string `arg:"positional"` Regexp bool `arg:"-e,--regexp" help:"Treat search string as a regexp"` Keys bool `arg:"-k,--keys" help:"Match against keys (true if -v is not specified)"` Values bool `arg:"-v,--values" help:"Match against values (true if -k is not specified)"` @@ -67,6 +67,9 @@ func (cmd *GrepCommand) Parse(args []string) error { if err != nil { return err } + if cmd.args.Path == "" { + cmd.args.Path = cmd.client.Pwd + } if cmd.args.Keys == true { cmd.Mode |= ModeKeys } diff --git a/cli/replace.go b/cli/replace.go index 71987f48..b9e7a0ad 100644 --- a/cli/replace.go +++ b/cli/replace.go @@ -23,7 +23,7 @@ type ReplaceCommand struct { type ReplaceCommandArgs struct { Search string `arg:"positional,required"` Replacement string `arg:"positional,required"` - Path string `arg:"positional,required"` + Path string `arg:"positional"` Regexp bool `arg:"-e,--regexp" help:"Treat search string and selector as a regexp"` KeySelector string `arg:"-s,--key-selector" help:"Limit replacements to specified key" placeholder:"PATTERN"` Keys bool `arg:"-k,--keys" help:"Match against keys (true if -v is not specified)"` @@ -85,6 +85,9 @@ func (cmd *ReplaceCommand) Parse(args []string) error { if err != nil { return err } + if cmd.args.Path == "" { + cmd.args.Path = cmd.client.Pwd + } if cmd.args.Keys == true { cmd.Mode |= ModeKeys } diff --git a/test/run-all-tests.sh b/test/run-all-tests.sh index e816c9b5..d279b5a4 100755 --- a/test/run-all-tests.sh +++ b/test/run-all-tests.sh @@ -1,7 +1,7 @@ #!/bin/bash set -e # required to fail test suite when a single test fails -VAULT_VERSIONS=("1.6.2" "1.0.0") +VAULT_VERSIONS=("1.6.3" "1.0.0") KV_BACKENDS=("KV1" "KV2") DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" diff --git a/test/run-single-test.sh b/test/run-single-test.sh index ce20586f..d902d339 100755 --- a/test/run-single-test.sh +++ b/test/run-single-test.sh @@ -1,7 +1,7 @@ #!/bin/bash set -e # required to fail test suite when a single test fails -VAULT_VERSION=${VAULT_VERSION:-"1.6.2"} +VAULT_VERSION=${VAULT_VERSION:-"1.6.3"} KV_BACKEND=${KV_BACKEND:-"KV2"} TEST_SUITE=${TEST_SUITE:-"commands/cp"} diff --git a/test/suites/commands/grep.bats b/test/suites/commands/grep.bats index 2c2a16c5..7504bb3b 100644 --- a/test/suites/commands/grep.bats +++ b/test/suites/commands/grep.bats @@ -97,6 +97,13 @@ load ../../bin/plugins/bats-assert/load assert_line --partial "unknown argument --foo" assert_failure 1 + ####################################### + echo "==== case: match in pwd ====" + export VAULT_PATH=${KV_BACKEND}/src/dev + run ${APP_BIN} -c "grep 'apple' -v" + unset VAULT_PATH + assert_line --partial "/${KV_BACKEND}/src/dev/1" + ####################################### echo "==== TODO case: grep term on directory with reduced permissions ====" diff --git a/test/suites/commands/replace.bats b/test/suites/commands/replace.bats index c92bfdd6..1aa740cf 100644 --- a/test/suites/commands/replace.bats +++ b/test/suites/commands/replace.bats @@ -150,4 +150,14 @@ load ../../bin/plugins/bats-assert/load run ${APP_BIN} -c "replace -e -s '][' '^apple' 'orange' ${KV_BACKEND}/src/selector/1 -y" assert_failure assert_line --partial "key-selector: error parsing regexp" + + ####################################### + echo "==== case: replace in pwd ====" + export VAULT_PATH=${KV_BACKEND}/src/a + run ${APP_BIN} -c "replace 'long-value' 'interesting-value' -y" + assert_success + assert_line "Writing!" + unset VAULT_PATH + run get_vault_value "long" "${KV_BACKEND}/src/a/foo" + assert_line this-is-a-really-interesting-value-for-testing }