From 76940361bdf7673e467ae51587174f13a6bd686e Mon Sep 17 00:00:00 2001 From: cuishuang Date: Fri, 4 Sep 2026 23:44:39 +0800 Subject: [PATCH] fix: disable file completion after chart URL prefixes Signed-off-by: cuishuang --- pkg/cmd/search_repo.go | 17 +++++++++++++---- pkg/cmd/search_repo_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/search_repo.go b/pkg/cmd/search_repo.go index 2d9d967f8..a787a8b5b 100644 --- a/pkg/cmd/search_repo.go +++ b/pkg/cmd/search_repo.go @@ -339,16 +339,25 @@ func compListCharts(toComplete string, includeFiles bool) ([]string, cobra.Shell cobra.CompDebugln(fmt.Sprintf("Completions after repos: %v", completions), settings.Debug) // Now handle completions for url prefixes - for _, url := range []string{"oci://\tChart OCI prefix", "https://\tChart URL prefix", "http://\tChart URL prefix", "file://\tChart local URL prefix"} { - if strings.HasPrefix(toComplete, url) { + urlPrefixes := []struct { + value string + desc string + }{ + {value: "oci://", desc: "Chart OCI prefix"}, + {value: "https://", desc: "Chart URL prefix"}, + {value: "http://", desc: "Chart URL prefix"}, + {value: "file://", desc: "Chart local URL prefix"}, + } + for _, prefix := range urlPrefixes { + if strings.HasPrefix(toComplete, prefix.value) { // The user already put in the full url prefix; we don't have // anything to add, but make sure the shell does not default // to file completion since we could be returning an empty array. noFile = true noSpace = true - } else if strings.HasPrefix(url, toComplete) { + } else if strings.HasPrefix(prefix.value, toComplete) { // We are completing a url prefix - completions = append(completions, url) + completions = append(completions, fmt.Sprintf("%s\t%s", prefix.value, prefix.desc)) noSpace = true } } diff --git a/pkg/cmd/search_repo_test.go b/pkg/cmd/search_repo_test.go index e28984fd8..cf30dfc85 100644 --- a/pkg/cmd/search_repo_test.go +++ b/pkg/cmd/search_repo_test.go @@ -17,7 +17,11 @@ limitations under the License. package cmd import ( + "path/filepath" "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" ) func TestSearchRepositoriesCmd(t *testing.T) { @@ -106,3 +110,29 @@ func TestSearchRepoOutputCompletion(t *testing.T) { func TestSearchRepoFileCompletion(t *testing.T) { checkFileCompletion(t, "search repo", true) // File completion may be useful when inputting a keyword } + +func TestCompListChartsURLPrefix(t *testing.T) { + originalRepositoryConfig := settings.RepositoryConfig + originalRepositoryCache := settings.RepositoryCache + settings.RepositoryConfig = filepath.Join(t.TempDir(), "repositories.yaml") + settings.RepositoryCache = t.TempDir() + t.Cleanup(func() { + settings.RepositoryConfig = originalRepositoryConfig + settings.RepositoryCache = originalRepositoryCache + }) + + tests := []struct { + name string + toComplete string + }{ + {name: "OCI URL", toComplete: "oci://registry.example/chart"}, + {name: "HTTPS URL", toComplete: "https://example.com/chart.tgz"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, directive := compListCharts(tt.toComplete, true) + assert.Equal(t, cobra.ShellCompDirectiveNoFileComp|cobra.ShellCompDirectiveNoSpace, directive) + }) + } +}