Skip to content

Commit

Permalink
Fix workload completion func.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbobrovskyi committed Sep 5, 2024
1 parent 0116a69 commit 1ae5e21
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
13 changes: 6 additions & 7 deletions cmd/kueuectl/app/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package completion

import (
"slices"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -100,10 +101,6 @@ func UsersFunc(clientGetter util.ClientGetter) func(*cobra.Command, []string, st

func WorkloadNameFunc(clientGetter util.ClientGetter, activeStatus *bool) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

clientSet, err := clientGetter.KueueClientSet()
if err != nil {
return []string{}, cobra.ShellCompDirectiveError
Expand All @@ -129,9 +126,11 @@ func WorkloadNameFunc(clientGetter util.ClientGetter, activeStatus *bool) func(*
list.Items = filteredItems
}

validArgs := make([]string, len(list.Items))
for i, wl := range list.Items {
validArgs[i] = wl.Name
var validArgs []string
for _, wl := range list.Items {
if !slices.Contains(args, wl.Name) {
validArgs = append(validArgs, wl.Name)
}
}

return validArgs, cobra.ShellCompDirectiveNoFileComp
Expand Down
3 changes: 2 additions & 1 deletion cmd/kueuectl/app/completion/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ func TestWorkloadNameCompletionFunc(t *testing.T) {
wantNames: []string{"wl2"},
wantDirective: cobra.ShellCompDirectiveNoFileComp,
},
"shouldn't return workload names because only one argument can be passed": {
"shouldn filter workload names": {
objs: []runtime.Object{
utiltesting.MakeWorkload("wl1", metav1.NamespaceDefault).Active(true).Obj(),
utiltesting.MakeWorkload("wl2", metav1.NamespaceDefault).Active(false).Obj(),
},
args: []string{"wl2"},
wantNames: []string{"wl1"},
wantDirective: cobra.ShellCompDirectiveNoFileComp,
},
}
Expand Down

0 comments on commit 1ae5e21

Please sign in to comment.