From e2101ab5e9cbdea814e330c8d3afe69378abd407 Mon Sep 17 00:00:00 2001 From: Willie Date: Mon, 30 Mar 2026 00:01:07 +0800 Subject: [PATCH] feat(cli): add resource kind filter parsing for CLI commands Add kind_filter.go with two parsing strategies: - parseKindArg(): positional syntax (list agents, sync skills) - parseKindFlag(): flag syntax (uninstall --kind agent tutor) resourceKindFilter type with IncludesSkills()/IncludesAgents() helpers for handler-level routing. Supports singular and plural forms (skill/skills, agent/agents). 16 test cases covering both parsers and filter logic. --- cmd/skillshare/kind_filter.go | 78 ++++++++++++++++++++++++++++++ cmd/skillshare/kind_filter_test.go | 76 +++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 cmd/skillshare/kind_filter.go create mode 100644 cmd/skillshare/kind_filter_test.go diff --git a/cmd/skillshare/kind_filter.go b/cmd/skillshare/kind_filter.go new file mode 100644 index 000000000..ec44159fc --- /dev/null +++ b/cmd/skillshare/kind_filter.go @@ -0,0 +1,78 @@ +package main + +import "fmt" + +// resourceKindFilter represents the kind filtering for CLI commands. +type resourceKindFilter int + +const ( + kindAll resourceKindFilter = iota // no filter — all kinds + kindSkills // skills only + kindAgents // agents only +) + +// parseKindArg extracts a kind filter from the first positional argument. +// Returns the filter and remaining args. +// Recognized values: "skills", "skill", "agents", "agent". +// If the first arg is not a kind keyword, returns kindAll with args unchanged. +func parseKindArg(args []string) (resourceKindFilter, []string) { + if len(args) == 0 { + return kindAll, args + } + + switch args[0] { + case "skills", "skill": + return kindSkills, args[1:] + case "agents", "agent": + return kindAgents, args[1:] + default: + return kindAll, args + } +} + +// parseKindFlag extracts --kind flag from args. +// Returns the filter and remaining args with --kind removed. +func parseKindFlag(args []string) (resourceKindFilter, []string, error) { + kind := kindAll + rest := make([]string, 0, len(args)) + + for i := 0; i < len(args); i++ { + if args[i] == "--kind" { + if i+1 >= len(args) { + return kindAll, nil, fmt.Errorf("--kind requires a value (skill or agent)") + } + i++ + switch args[i] { + case "skill", "skills": + kind = kindSkills + case "agent", "agents": + kind = kindAgents + default: + return kindAll, nil, fmt.Errorf("--kind must be 'skill' or 'agent', got %q", args[i]) + } + } else { + rest = append(rest, args[i]) + } + } + + return kind, rest, nil +} + +func (k resourceKindFilter) String() string { + switch k { + case kindSkills: + return "skills" + case kindAgents: + return "agents" + default: + return "all" + } +} + +func (k resourceKindFilter) IncludesSkills() bool { + return k == kindAll || k == kindSkills +} + +func (k resourceKindFilter) IncludesAgents() bool { + return k == kindAll || k == kindAgents +} diff --git a/cmd/skillshare/kind_filter_test.go b/cmd/skillshare/kind_filter_test.go new file mode 100644 index 000000000..3a9fb511e --- /dev/null +++ b/cmd/skillshare/kind_filter_test.go @@ -0,0 +1,76 @@ +package main + +import "testing" + +func TestParseKindArg(t *testing.T) { + tests := []struct { + args []string + wantKind resourceKindFilter + wantRest []string + }{ + {nil, kindAll, nil}, + {[]string{}, kindAll, []string{}}, + {[]string{"skills"}, kindSkills, []string{}}, + {[]string{"skill"}, kindSkills, []string{}}, + {[]string{"agents"}, kindAgents, []string{}}, + {[]string{"agent"}, kindAgents, []string{}}, + {[]string{"agents", "tutor"}, kindAgents, []string{"tutor"}}, + {[]string{"--json"}, kindAll, []string{"--json"}}, + {[]string{"my-skill"}, kindAll, []string{"my-skill"}}, + } + + for _, tt := range tests { + kind, rest := parseKindArg(tt.args) + if kind != tt.wantKind { + t.Errorf("parseKindArg(%v) kind = %v, want %v", tt.args, kind, tt.wantKind) + } + if len(rest) != len(tt.wantRest) { + t.Errorf("parseKindArg(%v) rest = %v, want %v", tt.args, rest, tt.wantRest) + } + } +} + +func TestParseKindFlag(t *testing.T) { + tests := []struct { + args []string + wantKind resourceKindFilter + wantRest []string + wantErr bool + }{ + {[]string{}, kindAll, []string{}, false}, + {[]string{"--kind", "agent"}, kindAgents, []string{}, false}, + {[]string{"--kind", "skill"}, kindSkills, []string{}, false}, + {[]string{"--json", "--kind", "agent", "foo"}, kindAgents, []string{"--json", "foo"}, false}, + {[]string{"--kind"}, kindAll, nil, true}, + {[]string{"--kind", "invalid"}, kindAll, nil, true}, + } + + for _, tt := range tests { + kind, rest, err := parseKindFlag(tt.args) + if (err != nil) != tt.wantErr { + t.Errorf("parseKindFlag(%v) err = %v, wantErr %v", tt.args, err, tt.wantErr) + continue + } + if err != nil { + continue + } + if kind != tt.wantKind { + t.Errorf("parseKindFlag(%v) kind = %v, want %v", tt.args, kind, tt.wantKind) + } + if len(rest) != len(tt.wantRest) { + t.Errorf("parseKindFlag(%v) rest = %v, want %v", tt.args, rest, tt.wantRest) + } + } +} + +func TestResourceKindFilter_Includes(t *testing.T) { + if !kindAll.IncludesSkills() || !kindAll.IncludesAgents() { + t.Error("kindAll should include both") + } + if !kindSkills.IncludesSkills() || kindSkills.IncludesAgents() { + t.Error("kindSkills should include only skills") + } + if kindAgents.IncludesSkills() || !kindAgents.IncludesAgents() { + t.Error("kindAgents should include only agents") + } +}