From c72720768373bff6d430db56d0bdf3dea764145b Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Sun, 28 Jun 2020 18:52:05 -0400 Subject: [PATCH] feat(comp): Add descriptions for repo completion Ref: HIP 0008 When completing a repo name, extra information will be shown. For example: $ helm repo remove bitnami -- https://charts.bitnami.com/bitnami center -- https://repo.chartcenter.io stable -- https://kubernetes-charts.storage.googleapis.com $ helm install myrelease ./ -- Relative path prefix to local chart / -- Absolute path prefix to local chart bitnami/ -- https://charts.bitnami.com/bitnami center/ -- https://repo.chartcenter.io file:// -- Chart local URL prefix grafana/ -- https://grafana.github.io/helm-charts https:// http:// -- Chart URL prefix oteemocharts/ -- https://oteemo.github.io/charts prometheus-community/ -- https://prometheus-community.github.io/helm-charts stable/ -- https://charts.helm.sh/stable $ helm show all bitnami -- https://charts.bitnami.com/bitnami center -- https://repo.chartcenter.io stable -- https://kubernetes-charts.storage.googleapis.com Signed-off-by: Marc Khouzam --- cmd/helm/repo_list.go | 3 +- cmd/helm/repo_remove_test.go | 46 +++++++++++++++++++ cmd/helm/search_repo.go | 17 +++++-- cmd/helm/testdata/output/repo_list_comp.txt | 5 ++ cmd/helm/testdata/output/repo_repeat_comp.txt | 4 ++ 5 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 cmd/helm/testdata/output/repo_list_comp.txt create mode 100644 cmd/helm/testdata/output/repo_repeat_comp.txt diff --git a/cmd/helm/repo_list.go b/cmd/helm/repo_list.go index fc53ba75a12..537f8bd2c72 100644 --- a/cmd/helm/repo_list.go +++ b/cmd/helm/repo_list.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "fmt" "io" "strings" @@ -131,7 +132,7 @@ func compListRepos(prefix string, ignoredRepoNames []string) []string { filteredRepos := filterRepos(f.Repositories, ignoredRepoNames) for _, repo := range filteredRepos { if strings.HasPrefix(repo.Name, prefix) { - rNames = append(rNames, repo.Name) + rNames = append(rNames, fmt.Sprintf("%s\t%s", repo.Name, repo.URL)) } } } diff --git a/cmd/helm/repo_remove_test.go b/cmd/helm/repo_remove_test.go index cb5c6e9ab0d..d9e77530fcc 100644 --- a/cmd/helm/repo_remove_test.go +++ b/cmd/helm/repo_remove_test.go @@ -18,6 +18,7 @@ package main import ( "bytes" + "fmt" "os" "path/filepath" "strings" @@ -161,6 +162,51 @@ func testCacheFiles(t *testing.T, cacheIndexFile string, cacheChartsFile string, } } +func TestRepoRemoveCompletion(t *testing.T) { + ts, err := repotest.NewTempServerWithCleanup(t, "testdata/testserver/*.*") + if err != nil { + t.Fatal(err) + } + defer ts.Stop() + + rootDir := ensure.TempDir(t) + repoFile := filepath.Join(rootDir, "repositories.yaml") + repoCache := filepath.Join(rootDir, "cache/") + + var testRepoNames = []string{"foo", "bar", "baz"} + + // Add test repos + for _, repoName := range testRepoNames { + o := &repoAddOptions{ + name: repoName, + url: ts.URL(), + repoFile: repoFile, + } + + if err := o.run(os.Stderr); err != nil { + t.Error(err) + } + } + + repoSetup := fmt.Sprintf("--repository-config %s --repository-cache %s", repoFile, repoCache) + + // In the following tests, we turn off descriptions for completions by using __completeNoDesc. + // We have to do this because the description will contain the port used by the webserver, + // and that port changes each time we run the test. + tests := []cmdTestCase{{ + name: "completion for repo remove", + cmd: fmt.Sprintf("%s __completeNoDesc repo remove ''", repoSetup), + golden: "output/repo_list_comp.txt", + }, { + name: "completion for repo remove repetition", + cmd: fmt.Sprintf("%s __completeNoDesc repo remove foo ''", repoSetup), + golden: "output/repo_repeat_comp.txt", + }} + for _, test := range tests { + runTestCmd(t, []cmdTestCase{test}) + } +} + func TestRepoRemoveFileCompletion(t *testing.T) { checkFileCompletion(t, "repo remove", false) checkFileCompletion(t, "repo remove repo1", false) diff --git a/cmd/helm/search_repo.go b/cmd/helm/search_repo.go index 3a5a2384816..ae60292a6f8 100644 --- a/cmd/helm/search_repo.go +++ b/cmd/helm/search_repo.go @@ -302,7 +302,14 @@ func compListCharts(toComplete string, includeFiles bool) ([]string, cobra.Shell // First check completions for repos repos := compListRepos("", nil) - for _, repo := range repos { + for _, repoInfo := range repos { + // Split name from description + repoInfo := strings.Split(repoInfo, "\t") + repo := repoInfo[0] + repoDesc := "" + if len(repoInfo) > 1 { + repoDesc = repoInfo[1] + } repoWithSlash := fmt.Sprintf("%s/", repo) if strings.HasPrefix(toComplete, repoWithSlash) { // Must complete with charts within the specified repo @@ -310,15 +317,15 @@ func compListCharts(toComplete string, includeFiles bool) ([]string, cobra.Shell noSpace = false break } else if strings.HasPrefix(repo, toComplete) { - // Must complete the repo name - completions = append(completions, repoWithSlash) + // Must complete the repo name with the slash, followed by the description + completions = append(completions, fmt.Sprintf("%s\t%s", repoWithSlash, repoDesc)) noSpace = true } } cobra.CompDebugln(fmt.Sprintf("Completions after repos: %v", completions), settings.Debug) // Now handle completions for url prefixes - for _, url := range []string{"https://", "http://", "file://"} { + for _, url := range []string{"https://\tChart URL prefix", "http://\tChart URL prefix", "file://\tChart local URL prefix"} { if strings.HasPrefix(toComplete, url) { // The user already put in the full url prefix; we don't have // anything to add, but make sure the shell does not default @@ -355,7 +362,7 @@ func compListCharts(toComplete string, includeFiles bool) ([]string, cobra.Shell // If the user didn't provide any input to completion, // we provide a hint that a path can also be used if includeFiles && len(toComplete) == 0 { - completions = append(completions, "./", "/") + completions = append(completions, "./\tRelative path prefix to local chart", "/\tAbsolute path prefix to local chart") } cobra.CompDebugln(fmt.Sprintf("Completions after checking empty input: %v", completions), settings.Debug) diff --git a/cmd/helm/testdata/output/repo_list_comp.txt b/cmd/helm/testdata/output/repo_list_comp.txt new file mode 100644 index 00000000000..289e0d2e199 --- /dev/null +++ b/cmd/helm/testdata/output/repo_list_comp.txt @@ -0,0 +1,5 @@ +foo +bar +baz +:4 +Completion ended with directive: ShellCompDirectiveNoFileComp diff --git a/cmd/helm/testdata/output/repo_repeat_comp.txt b/cmd/helm/testdata/output/repo_repeat_comp.txt new file mode 100644 index 00000000000..ed8ed89fa54 --- /dev/null +++ b/cmd/helm/testdata/output/repo_repeat_comp.txt @@ -0,0 +1,4 @@ +bar +baz +:4 +Completion ended with directive: ShellCompDirectiveNoFileComp