diff --git a/CHANGELOG.md b/CHANGELOG.md index ca637da8..3541c689 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## v0.7.0 (September 26, 2020) + +ENHANCEMENTS: + +* Add option to disable path auto-completion ([#48](https://github.com/fishi0x01/vsh/pull/48)) + ## v0.6.3 (September 22, 2020) BUG FIXES: diff --git a/Makefile b/Makefile index 7ecf1115..95d35fad 100644 --- a/Makefile +++ b/Makefile @@ -22,11 +22,14 @@ get-bats: mkdir -p test/bin/plugins/bats-assert mkdir -p test/bin/plugins/bats-support curl -sL https://github.com/bats-core/bats-core/archive/v1.2.0.tar.gz | tar xvz --strip 1 -C test/bin/core - curl -sL https://github.com/bats-core/bats-assert/archive/v2.0.0.tar.gz | tar xvz --strip 1 -C test/bin/plugins/bats-assert - curl -sL https://github.com/bats-core/bats-support/archive/v0.3.0.tar.gz | tar xvz --strip 1 -C test/bin/plugins/bats-support + curl -sL https://github.com/bats-core/bats-assert/archive/v2.0.0.tar.gz | tar xvz --strip 1 -C test/bin/plugins/bats-assert + curl -sL https://github.com/bats-core/bats-support/archive/v0.3.0.tar.gz | tar xvz --strip 1 -C test/bin/plugins/bats-support integration-tests: test/run.sh +local-vault-test-instance: + bash -c ". test/util/util.bash && setup" + clean: rm ./build/* || true diff --git a/README.md b/README.md index ae61f30c..6a8a2749 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,24 @@ http://localhost:8080 /secret/> in order to do accurate tab-completion. If your token has a limited number of uses, then consider using the non-interactive mode to avoid auto-completion queries. +### Toggle auto-completion + +To reduce the number of queries against vault, you can disable path auto-completion in 2 ways: + +1. Disable at start time: +``` +./vsh --disable-auto-completion +``` + +2. Toggle inside interactive mode: +``` +./vsh +http://localhost:8080 /secret/> toggle-auto-completion +Use path auto-completion: false +http://localhost:8080 /secret/> toggle-auto-completion +Use path auto-completion: true +``` + ## Non-interactive mode ``` @@ -150,15 +168,15 @@ export VAULT_TOKEN= ## Permission requirements -`vsh` requires `List` permission on the operated paths. -This is necessary to determine if a path points to a node or leaf in the path tree. +`vsh` requires `List` permission on the operated paths. +This is necessary to determine if a path points to a node or leaf in the path tree. Further, it is needed to gather auto-completion data. For operations like `cp` or `mv`, `vsh` additionally requires `Read` and `Write` permissions on the operated paths. ## Stability -Every command has integration tests against KV1 and KV2. +Every command has integration tests against KV1 and KV2. Every test is run against vault `1.0.0` and `1.5.3`, i.e., versions in between should also be compatible. ## Local Development diff --git a/completer/completer.go b/completer/completer.go index 4bb1e357..bc4a0367 100644 --- a/completer/completer.go +++ b/completer/completer.go @@ -1,6 +1,7 @@ package completer import ( + "fmt" "strings" "github.com/c-bata/go-prompt" @@ -9,16 +10,24 @@ import ( // Completer struct for tab completion type Completer struct { - client *client.Client + pathCompletionToggle bool + client *client.Client } // NewCompleter creates a new Completer with given client -func NewCompleter(client *client.Client) *Completer { +func NewCompleter(client *client.Client, disableAutoCompletion bool) *Completer { return &Completer{ - client: client, + pathCompletionToggle: !disableAutoCompletion, + client: client, } } +// TogglePathCompletion enable/disable path auto-completion +func (c *Completer) TogglePathCompletion() { + c.pathCompletionToggle = !c.pathCompletionToggle + fmt.Printf("Use path auto-completion: %t\n", c.pathCompletionToggle) +} + func (c *Completer) getAbsoluteTopLevelSuggestions() []prompt.Suggest { var suggestions []prompt.Suggest for k := range c.client.KVBackends { @@ -108,7 +117,8 @@ func isCommandArgument(p string) bool { words[0] == "grep" || words[0] == "cat" || words[0] == "append" || - words[0] == "ls" + words[0] == "ls" || + words[0] == "toggle-auto-completion" } func isCommand(p string) bool { @@ -125,6 +135,7 @@ func (c *Completer) commandSuggestions(arg string) (result []prompt.Suggest) { {Text: "grep", Description: "grep "}, {Text: "cat", Description: "cat "}, {Text: "ls", Description: "ls "}, + {Text: "toggle-auto-completion", Description: "toggle path auto-completion on/off"}, } filtered := prompt.FilterHasPrefix(result, arg, true) if len(filtered) > 0 { @@ -138,7 +149,7 @@ func (c *Completer) Complete(in prompt.Document) (result []prompt.Suggest) { p := in.TextBeforeCursor() if isCommand(p) { result = c.commandSuggestions(in.GetWordBeforeCursor()) - } else if isCommandArgument(p) { + } else if isCommandArgument(p) && c.pathCompletionToggle { cur := in.GetWordBeforeCursor() if isAbsolutePath(cur) { result = c.absolutePathSuggestions(cur) diff --git a/main.go b/main.go index 85c3a0c2..0b7521b9 100644 --- a/main.go +++ b/main.go @@ -54,6 +54,8 @@ func parseInput(line string) (args []string) { return strings.Fields(line) } +var completerInstance *completer.Completer + func executor(in string) { // Every command can change the vault content // i.e., the cache should be cleared on command execution @@ -97,6 +99,8 @@ func executor(in string) { case commands.grep.GetName(): run = commands.grep.Parse(args) cmd = commands.grep + case "toggle-auto-completion": + completerInstance.TogglePathCompletion() case "exit": os.Exit(0) default: @@ -132,8 +136,10 @@ func main() { var cmdString string var hasVersion bool + var disableAutoCompletion bool flag.StringVar(&cmdString, "c", "", "command to run") flag.BoolVar(&hasVersion, "version", false, "print vsh version") + flag.BoolVar(&disableAutoCompletion, "disable-auto-completion", false, "disable auto-completion on paths") flag.BoolVar(&verbose, "v", false, "verbose output") flag.Parse() @@ -171,12 +177,12 @@ func main() { executor(cmdString) } else { // Run interactive mode - completer := completer.NewCompleter(vaultClient) + completerInstance = completer.NewCompleter(vaultClient, disableAutoCompletion) p := prompt.New( executor, - completer.Complete, + completerInstance.Complete, prompt.OptionTitle("vsh - interactive vault shell"), - prompt.OptionLivePrefix(completer.PromptPrefix), + prompt.OptionLivePrefix(completerInstance.PromptPrefix), prompt.OptionInputTextColor(prompt.Yellow), prompt.OptionShowCompletionAtStart(), ) diff --git a/test/util/util.bash b/test/util/util.bash index 9c555173..be93ae95 100755 --- a/test/util/util.bash +++ b/test/util/util.bash @@ -1,7 +1,6 @@ #!/bin/bash -export VAULT_VERSION=${VAULT_VERSION} -export KV_BACKEND=${KV_BACKEND} +export VAULT_VERSION=${VAULT_VERSION:-"1.5.3"} export VAULT_CONTAINER_NAME="vsh-integration-test-vault" export VAULT_HOST_PORT=${VAULT_HOST_PORT:-"8888"}