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

$ helm install myrelease <TAB>
./                              -- 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 <TAB>
bitnami  -- https://charts.bitnami.com/bitnami
center   -- https://repo.chartcenter.io
stable   -- https://kubernetes-charts.storage.googleapis.com

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
  • Loading branch information
marckhouzam committed Jul 31, 2021
1 parent e9e6b07 commit 024bb3e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 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
17 changes: 12 additions & 5 deletions cmd/helm/search_repo.go
Expand Up @@ -302,23 +302,30 @@ 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
completions = append(completions, compListChartsOfRepo(repo, toComplete)...)
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
Expand Down Expand Up @@ -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)

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 024bb3e

Please sign in to comment.