Skip to content

Commit

Permalink
feat(comp): Add descriptions for repo completion
Browse files Browse the repository at this point in the history
Ref: HIP 0008

When completing a repo name, extra information will be shown for
shells that support completions (fish, zsh).  For example:

$ helm repo remove <TAB>
bitnami  -- https://charts.bitnami.com/bitnami
center   -- https://repo.chartcenter.io
stable   -- https://kubernetes-charts.storage.googleapis.com

This commit does not add the description to repo names for the commands:
intall, template, upgrade, show and pull.  This is because we first need
a couple of Cobra fixes:
spf13/cobra#1211
spf13/cobra#1248

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
  • Loading branch information
marckhouzam committed Feb 27, 2021
1 parent 3e31983 commit d91448f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cmd/helm/repo_list.go
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"fmt"
"io"
"strings"

Expand Down Expand Up @@ -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))
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions cmd/helm/repo_remove_test.go
Expand Up @@ -18,6 +18,7 @@ package main

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions cmd/helm/search_repo.go
Expand Up @@ -301,15 +301,22 @@ 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]
repoWithSlash := fmt.Sprintf("%s/", repo)
if strings.HasPrefix(toComplete, repoWithSlash) {
// Must complete with charts within the specified repo
completions = append(completions, compListChartsOfRepo(repo, toComplete)...)
noSpace = false
break
} else if strings.HasPrefix(repo, toComplete) {
// Must complete the repo name
// Must complete the repo name with the slash, followed by the description
// TODO Actually add description once Cobra properly handles descriptions
// with ShellCompDirectiveNoSpace
// https://github.com/spf13/cobra/issues/1211
// https://github.com/spf13/cobra/issues/1248
completions = append(completions, repoWithSlash)
noSpace = true
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/helm/testdata/output/repo_list_comp.txt
@@ -0,0 +1,5 @@
foo
bar
baz
:4
Completion ended with directive: ShellCompDirectiveNoFileComp
4 changes: 4 additions & 0 deletions cmd/helm/testdata/output/repo_repeat_comp.txt
@@ -0,0 +1,4 @@
bar
baz
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

0 comments on commit d91448f

Please sign in to comment.