fix: update SuggestSubCommand to pass command context for suggestions (#3516)

mine
Ümüt Özalp 2025-08-20 16:46:50 +02:00 committed by GitHub
parent 2149e3f546
commit 67a637f7be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 5 deletions

View File

@ -58,11 +58,11 @@ func SuggestSubCommand(command string, namespaces client.NamespaceNames, context
if !ok { if !ok {
return nil return nil
} }
suggests = completeCtx(n, contexts) suggests = completeCtx(command, n, contexts)
case p.HasNS(): case p.HasNS():
if n, ok := p.HasContext(); ok { if n, ok := p.HasContext(); ok {
suggests = completeCtx(n, contexts) suggests = completeCtx(command, n, contexts)
} }
if len(suggests) > 0 { if len(suggests) > 0 {
break break
@ -76,7 +76,7 @@ func SuggestSubCommand(command string, namespaces client.NamespaceNames, context
default: default:
if n, ok := p.HasContext(); ok { if n, ok := p.HasContext(); ok {
suggests = completeCtx(n, contexts) suggests = completeCtx(command, n, contexts)
} }
} }
slices.Sort(suggests) slices.Sort(suggests)
@ -99,11 +99,11 @@ func completeNS(s string, nn client.NamespaceNames) []string {
return suggests return suggests
} }
func completeCtx(s string, contexts []string) []string { func completeCtx(command, s string, contexts []string) []string {
var suggests []string var suggests []string
for _, ctxName := range contexts { for _, ctxName := range contexts {
if suggest, ok := ShouldAddSuggest(s, ctxName); ok { if suggest, ok := ShouldAddSuggest(s, ctxName); ok {
if s == "" { if s == "" && !strings.HasSuffix(command, " ") {
suggests = append(suggests, " "+suggest) suggests = append(suggests, " "+suggest)
continue continue
} }

View File

@ -77,6 +77,8 @@ func TestSuggestSubCommand(t *testing.T) {
{Command: "ctx p", Suggestions: []string{"re", "rod"}}, {Command: "ctx p", Suggestions: []string{"re", "rod"}},
{Command: "ctx p", Suggestions: []string{"re", "rod"}}, {Command: "ctx p", Suggestions: []string{"re", "rod"}},
{Command: "ctx pr", Suggestions: []string{"e", "od"}}, {Command: "ctx pr", Suggestions: []string{"e", "od"}},
{Command: "ctx", Suggestions: []string{" develop", " pre", " prod", " test"}},
{Command: "ctx ", Suggestions: []string{"develop", "pre", "prod", "test"}},
{Command: "context d", Suggestions: []string{"evelop"}}, {Command: "context d", Suggestions: []string{"evelop"}},
{Command: "contexts t", Suggestions: []string{"est"}}, {Command: "contexts t", Suggestions: []string{"est"}},
{Command: "po ", Suggestions: nil}, {Command: "po ", Suggestions: nil},