Support CLI autocompletion for nested mounts (#8303)

* Support CLI autocompletion for nested mounts

* Add test for nested autocomplete prediction
This commit is contained in:
Daniel Spangenberg 2020-02-07 15:58:30 +01:00 committed by GitHub
parent fbd4925889
commit f84288d74f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -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)

View File

@ -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) {