fix: disable file completion after chart URL prefixes

Signed-off-by: cuishuang <imcusg@gmail.com>
pull/32627/head
cuishuang 6 days ago
parent fa11636b01
commit 76940361bd

@ -339,16 +339,25 @@ func compListCharts(toComplete string, includeFiles bool) ([]string, cobra.Shell
cobra.CompDebugln(fmt.Sprintf("Completions after repos: %v", completions), settings.Debug) cobra.CompDebugln(fmt.Sprintf("Completions after repos: %v", completions), settings.Debug)
// Now handle completions for url prefixes // 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"} { urlPrefixes := []struct {
if strings.HasPrefix(toComplete, url) { 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 // The user already put in the full url prefix; we don't have
// anything to add, but make sure the shell does not default // anything to add, but make sure the shell does not default
// to file completion since we could be returning an empty array. // to file completion since we could be returning an empty array.
noFile = true noFile = true
noSpace = true noSpace = true
} else if strings.HasPrefix(url, toComplete) { } else if strings.HasPrefix(prefix.value, toComplete) {
// We are completing a url prefix // We are completing a url prefix
completions = append(completions, url) completions = append(completions, fmt.Sprintf("%s\t%s", prefix.value, prefix.desc))
noSpace = true noSpace = true
} }
} }

@ -17,7 +17,11 @@ limitations under the License.
package cmd package cmd
import ( import (
"path/filepath"
"testing" "testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
) )
func TestSearchRepositoriesCmd(t *testing.T) { func TestSearchRepositoriesCmd(t *testing.T) {
@ -106,3 +110,29 @@ func TestSearchRepoOutputCompletion(t *testing.T) {
func TestSearchRepoFileCompletion(t *testing.T) { func TestSearchRepoFileCompletion(t *testing.T) {
checkFileCompletion(t, "search repo", true) // File completion may be useful when inputting a keyword 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)
})
}
}

Loading…
Cancel
Save