From 3f97731b22eb1616d0b6b64e661dc28a644ae003 Mon Sep 17 00:00:00 2001 From: Daniel Spangenberg Date: Thu, 6 Feb 2020 00:03:11 +0100 Subject: [PATCH 1/2] Support CLI autocompletion for nested mounts --- command/base_predict.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/command/base_predict.go b/command/base_predict.go index f8fee0ae1ec3..c7b306118a3f 100644 --- a/command/base_predict.go +++ b/command/base_predict.go @@ -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) From b887cba5dde167ed9c0a5c3840b5dd963a7e68ff Mon Sep 17 00:00:00 2001 From: Daniel Spangenberg Date: Fri, 7 Feb 2020 01:27:54 +0100 Subject: [PATCH 2/2] Add test for nested autocomplete prediction --- command/base_predict_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/command/base_predict_test.go b/command/base_predict_test.go index 9523dc938d6a..2e06b6f5ec93 100644 --- a/command/base_predict_test.go +++ b/command/base_predict_test.go @@ -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) {