From ffdded311ff9ec601030befc6866447fc4b0ddee Mon Sep 17 00:00:00 2001 From: Willie Date: Thu, 26 Mar 2026 23:42:14 +0800 Subject: [PATCH] feat(analyze): add --filter flag with filtered_summary in JSON output Support case-insensitive substring filter on skill relPath. When --filter is set, JSON output includes filtered_summary with always_loaded, on_demand, and total token counts for matched skills. Ref: Discussion #99 --- cmd/skillshare/analyze.go | 96 ++++++++++++++++++++++++-- tests/integration/analyze_test.go | 108 ++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+), 7 deletions(-) diff --git a/cmd/skillshare/analyze.go b/cmd/skillshare/analyze.go index 6b560bb9c..9f138b1ed 100644 --- a/cmd/skillshare/analyze.go +++ b/cmd/skillshare/analyze.go @@ -17,6 +17,7 @@ type analyzeOptions struct { verbose bool json bool noTUI bool + filter string } type analyzeSkillEntry struct { @@ -56,9 +57,49 @@ type analyzeLoadResult struct { err error } +type analyzeFilteredOutput struct { + Filter string `json:"filter"` + MatchedCount int `json:"matched_count"` + TotalCount int `json:"total_count"` + FilteredSummary analyzeFilterSummary `json:"filtered_summary"` + Skills []analyzeSkillEntry `json:"skills"` +} + +type analyzeFilterSummary struct { + AlwaysLoaded analyzeFilterTokens `json:"always_loaded"` + OnDemand analyzeFilterTokens `json:"on_demand"` + Total analyzeFilterTokens `json:"total"` +} + +type analyzeFilterTokens struct { + Chars int `json:"chars"` + Tokens int `json:"tokens"` +} + +func filterAnalyzeSkills(skills []analyzeSkillEntry, filter string) (matched []analyzeSkillEntry, summary analyzeFilterSummary) { + lower := strings.ToLower(filter) + for _, s := range skills { + searchField := s.relPath + if searchField == "" { + searchField = s.Name + } + if strings.Contains(strings.ToLower(searchField), lower) { + matched = append(matched, s) + summary.AlwaysLoaded.Chars += s.DescriptionChars + summary.OnDemand.Chars += s.BodyChars + } + } + summary.AlwaysLoaded.Tokens = estimateTokens(summary.AlwaysLoaded.Chars) + summary.OnDemand.Tokens = estimateTokens(summary.OnDemand.Chars) + summary.Total.Chars = summary.AlwaysLoaded.Chars + summary.OnDemand.Chars + summary.Total.Tokens = estimateTokens(summary.Total.Chars) + return +} + func parseAnalyzeArgs(args []string) (*analyzeOptions, bool, error) { opts := &analyzeOptions{} - for _, arg := range args { + for i := 0; i < len(args); i++ { + arg := args[i] switch { case arg == "--verbose" || arg == "-v": opts.verbose = true @@ -66,6 +107,17 @@ func parseAnalyzeArgs(args []string) (*analyzeOptions, bool, error) { opts.json = true case arg == "--no-tui": opts.noTUI = true + case arg == "--filter": + if i+1 >= len(args) { + return nil, false, fmt.Errorf("--filter requires a value") + } + i++ + opts.filter = args[i] + case strings.HasPrefix(arg, "--filter="): + opts.filter = strings.TrimPrefix(arg, "--filter=") + if opts.filter == "" { + return nil, false, fmt.Errorf("--filter requires a non-empty value") + } case arg == "--help" || arg == "-h": return nil, true, nil case strings.HasPrefix(arg, "-"): @@ -127,18 +179,20 @@ Arguments: target Show details for a single target (optional) Options: - --verbose, -v Show top 10 largest descriptions per target - --project, -p Analyze project-level skills (.skillshare/) - --global, -g Analyze global skills (~/.config/skillshare) - --json Output results as JSON - --no-tui Disable interactive TUI - --help, -h Show this help + --verbose, -v Show top 10 largest descriptions per target + --project, -p Analyze project-level skills (.skillshare/) + --global, -g Analyze global skills (~/.config/skillshare) + --json Output results as JSON + --filter Filter skills by name/path (case-insensitive substring) + --no-tui Disable interactive TUI + --help, -h Show this help Examples: skillshare analyze # Summary table for all targets skillshare analyze --verbose # Top 10 descriptions per target skillshare analyze claude # Details for claude target skillshare analyze --json # JSON output + skillshare analyze --filter api # Show only skills matching "api" skillshare analyze -p # Project mode`) } @@ -209,9 +263,37 @@ func runAnalyzeCore(sourcePath string, targets map[string]config.TargetConfig, d } if opts.json { + if opts.filter != "" && len(entries) > 0 { + entry := entries[0] + matched, summary := filterAnalyzeSkills(entry.Skills, opts.filter) + return writeJSON(&analyzeFilteredOutput{ + Filter: opts.filter, + MatchedCount: len(matched), + TotalCount: len(entry.Skills), + FilteredSummary: summary, + Skills: matched, + }) + } else if opts.filter != "" { + return writeJSON(&analyzeFilteredOutput{Filter: opts.filter}) + } return writeJSON(&analyzeOutput{Targets: entries}) } + if opts.filter != "" { + for i, entry := range entries { + matched, _ := filterAnalyzeSkills(entry.Skills, opts.filter) + entries[i].Skills = matched + entries[i].SkillCount = len(matched) + var descChars, bodyChars int + for _, s := range matched { + descChars += s.DescriptionChars + bodyChars += s.BodyChars + } + entries[i].AlwaysLoaded = analyzeCharTokens{Chars: descChars, EstimatedTokens: estimateTokens(descChars)} + entries[i].OnDemandMax = analyzeCharTokens{Chars: bodyChars, EstimatedTokens: estimateTokens(bodyChars)} + } + } + if opts.verbose { printAnalyzeVerbose(entries) } else { diff --git a/tests/integration/analyze_test.go b/tests/integration/analyze_test.go index 9711bb928..abc39bc24 100644 --- a/tests/integration/analyze_test.go +++ b/tests/integration/analyze_test.go @@ -316,6 +316,114 @@ func TestAnalyze_HelpShowsNoTUI(t *testing.T) { result.AssertOutputContains(t, "--no-tui") } +func TestAnalyze_JSONFilter(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + // Create skills in different groups — use CreateNestedSkill + sb.CreateNestedSkill("frontend/react", map[string]string{ + "SKILL.md": "---\nname: react\ndescription: React best practices for building UIs\n---\n# React\nBody content here", + }) + sb.CreateNestedSkill("frontend/vue", map[string]string{ + "SKILL.md": "---\nname: vue\ndescription: Vue framework patterns\n---\n# Vue\nVue body", + }) + sb.CreateNestedSkill("backend/go", map[string]string{ + "SKILL.md": "---\nname: go\ndescription: Go patterns and idioms\n---\n# Go\nGo body content", + }) + + target := sb.CreateTarget("claude") + sb.WriteConfig(`source: ` + sb.SourcePath + ` +targets: + claude: + path: ` + target + ` +`) + + result := sb.RunCLI("analyze", "claude", "--json", "--filter", "frontend") + result.AssertSuccess(t) + + var output struct { + Filter string `json:"filter"` + MatchedCount int `json:"matched_count"` + TotalCount int `json:"total_count"` + FilteredSummary struct { + AlwaysLoaded struct { + Chars int `json:"chars"` + Tokens int `json:"tokens"` + } `json:"always_loaded"` + OnDemand struct { + Chars int `json:"chars"` + Tokens int `json:"tokens"` + } `json:"on_demand"` + Total struct { + Chars int `json:"chars"` + Tokens int `json:"tokens"` + } `json:"total"` + } `json:"filtered_summary"` + Skills []struct { + Name string `json:"name"` + } `json:"skills"` + } + if err := json.Unmarshal([]byte(result.Stdout), &output); err != nil { + t.Fatalf("failed to parse JSON: %v\noutput: %s", err, result.Stdout) + } + if output.Filter != "frontend" { + t.Errorf("expected filter 'frontend', got %q", output.Filter) + } + if output.MatchedCount != 2 { + t.Errorf("expected 2 matched skills, got %d", output.MatchedCount) + } + if output.TotalCount != 3 { + t.Errorf("expected 3 total skills, got %d", output.TotalCount) + } + if len(output.Skills) != 2 { + t.Fatalf("expected 2 skills in array, got %d", len(output.Skills)) + } + if output.FilteredSummary.AlwaysLoaded.Chars <= 0 { + t.Errorf("expected positive always_loaded.chars, got %d", output.FilteredSummary.AlwaysLoaded.Chars) + } + if output.FilteredSummary.Total.Chars <= 0 { + t.Errorf("expected positive total.chars, got %d", output.FilteredSummary.Total.Chars) + } +} + +func TestAnalyze_JSONFilterNoMatch(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + sb.CreateSkill("skill1", map[string]string{ + "SKILL.md": "---\nname: skill1\ndescription: A test skill\n---\n# Skill 1\nBody", + }) + target := sb.CreateTarget("claude") + sb.WriteConfig(`source: ` + sb.SourcePath + ` +targets: + claude: + path: ` + target + ` +`) + + result := sb.RunCLI("analyze", "claude", "--json", "--filter", "nonexistent") + result.AssertSuccess(t) + + var output struct { + MatchedCount int `json:"matched_count"` + TotalCount int `json:"total_count"` + Skills []struct { + Name string `json:"name"` + } `json:"skills"` + } + if err := json.Unmarshal([]byte(result.Stdout), &output); err != nil { + t.Fatalf("failed to parse JSON: %v\noutput: %s", err, result.Stdout) + } + if output.MatchedCount != 0 { + t.Errorf("expected 0 matched skills, got %d", output.MatchedCount) + } + if output.TotalCount != 1 { + t.Errorf("expected 1 total skill, got %d", output.TotalCount) + } + if len(output.Skills) != 0 { + t.Fatalf("expected 0 skills in array, got %d", len(output.Skills)) + } +} + func TestAnalyze_JSONLintIssues(t *testing.T) { sb := testutil.NewSandbox(t) defer sb.Cleanup()