Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions cmd/kfeatures/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,43 @@ func (o *CheckOptions) DecodeRequire(input any) (any, error) {
return parseFeatureRequirements(s)
}

func (o *CheckOptions) CompleteRequire(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
prefix := ""
current := toComplete
selected := map[string]struct{}{}

if comma := strings.LastIndex(toComplete, ","); comma >= 0 {
prefix = toComplete[:comma+1]
current = toComplete[comma+1:]

for _, raw := range strings.Split(toComplete[:comma], ",") {
raw = strings.TrimSpace(raw)
if raw == "" {
continue
}

var f kfeatures.Feature
if err := f.UnmarshalText([]byte(raw)); err == nil {
selected[f.String()] = struct{}{}
}
}
}

current = strings.ToLower(strings.TrimSpace(current))
candidates := make([]string, 0, len(kfeatures.FeatureNames()))
for _, name := range kfeatures.FeatureNames() {
if _, ok := selected[name]; ok {
continue
}
if current != "" && !strings.HasPrefix(strings.ToLower(name), current) {
continue
}
candidates = append(candidates, prefix+name)
}

return candidates, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
}

func checkCmd() *cobra.Command {
opts := &CheckOptions{}

Expand Down
45 changes: 45 additions & 0 deletions cmd/kfeatures/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/leodido/kfeatures"
"github.com/spf13/cobra"
)

func TestParseFeatureRequirements_CaseInsensitive(t *testing.T) {
Expand Down Expand Up @@ -66,3 +67,47 @@ func TestCheckLongDescription_UsesEnumNames(t *testing.T) {
}
}
}

func TestCheckOptionsCompleteRequire(t *testing.T) {
opts := &CheckOptions{}

t.Run("empty input returns feature candidates", func(t *testing.T) {
got, directive := opts.CompleteRequire(nil, nil, "")
if len(got) == 0 {
t.Fatal("expected non-empty candidates")
}
if got[0] != kfeatures.FeatureNames()[0] {
t.Fatalf("first candidate = %q, want %q", got[0], kfeatures.FeatureNames()[0])
}
if directive != cobra.ShellCompDirectiveNoFileComp|cobra.ShellCompDirectiveNoSpace {
t.Fatalf("directive = %v, want %v", directive, cobra.ShellCompDirectiveNoFileComp|cobra.ShellCompDirectiveNoSpace)
}
})

t.Run("prefix filter is case-insensitive", func(t *testing.T) {
got, _ := opts.CompleteRequire(nil, nil, "BPF-S")
if len(got) == 0 {
t.Fatal("expected filtered candidates")
}
for _, c := range got {
if !strings.HasPrefix(c, "bpf-s") {
t.Fatalf("candidate %q does not match expected prefix", c)
}
}
})

t.Run("comma-separated completion prefixes and avoids duplicates", func(t *testing.T) {
got, _ := opts.CompleteRequire(nil, nil, "BPF-SYSCALL,tr")
if len(got) == 0 {
t.Fatal("expected comma-separated candidates")
}
for _, c := range got {
if !strings.HasPrefix(c, "BPF-SYSCALL,") {
t.Fatalf("candidate %q missing expected prefix", c)
}
if strings.EqualFold(c, "BPF-SYSCALL,bpf-syscall") {
t.Fatalf("duplicate selected feature suggested: %q", c)
}
}
})
}
14 changes: 14 additions & 0 deletions test/cli_common.bats
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,17 @@ setup_file() {
assert_failure
assert_output --partial 'unknown feature: "bpffs"'
}

@test "completion: check require suggests feature values" {
run "$KFEATURES_BIN" __complete check --require ""
assert_success
assert_output --partial "bpf-lsm"
}

@test "completion: check require supports comma-separated values" {
run "$KFEATURES_BIN" __complete check --require "BPF-SYSCALL,tr"
assert_success
assert_output --partial "BPF-SYSCALL,tracepoint"
assert_output --partial "BPF-SYSCALL,trace-fs"
refute_output --partial "BPF-SYSCALL,bpf-syscall"
}
Loading