Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CLI autocompletion for nested mounts #8303

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion command/base_predict.go
Original file line number Diff line number Diff line change
@@ -233,8 +233,18 @@ func (p *Predict) vaultPaths(includeFiles bool) complete.PredictFunc {

path := args.Last

// Trim path with potential mount
var relativePath string
for _, mount := range p.mounts() {
if strings.HasPrefix(path, mount) {
relativePath = strings.TrimPrefix(path, mount+"/")
break
}
}

// Predict path or mount depending on path separator
var predictions []string
if strings.Contains(path, "/") {
if strings.Contains(relativePath, "/") {
predictions = p.paths(path, includeFiles)
} else {
predictions = p.filter(p.mounts(), path)
18 changes: 18 additions & 0 deletions command/base_predict_test.go
Original file line number Diff line number Diff line change
@@ -31,6 +31,12 @@ func TestPredictVaultPaths(t *testing.T) {
if _, err := client.Logical().Write("secret/zip/twoot", data); err != nil {
t.Fatal(err)
}
if err := client.Sys().Mount("level1a/level2a/level3a", &api.MountInput{Type: "kv"}); err != nil {
t.Fatal(err)
}
if err := client.Sys().Mount("level1a/level2a/level3b", &api.MountInput{Type: "kv"}); err != nil {
t.Fatal(err)
}

cases := []struct {
name string
@@ -182,6 +188,18 @@ func TestPredictVaultPaths(t *testing.T) {
false,
[]string{"secret/zip/t"},
},
{
"multi_nested",
complete.Args{
All: []string{"read", "level1a/level2a"},
Last: "level1a/level2a",
},
false,
[]string{
"level1a/level2a/level3a/",
"level1a/level2a/level3b/",
},
},
}

t.Run("group", func(t *testing.T) {