diff --git a/cmd/skillshare/search.go b/cmd/skillshare/search.go index c63a33aa4..4474d8925 100644 --- a/cmd/skillshare/search.go +++ b/cmd/skillshare/search.go @@ -42,28 +42,46 @@ func cmdSearch(args []string) error { var query string var jsonOutput bool var listOnly bool + var indexURL string var limit int = 20 // Parse remaining arguments i := 0 for i < len(rest) { arg := rest[i] + key, val, hasEq := strings.Cut(arg, "=") switch { - case arg == "--json": + case key == "--json": jsonOutput = true - case arg == "--list" || arg == "-l": + case key == "--list" || key == "-l": listOnly = true - case arg == "--limit" || arg == "-n": - if i+1 >= len(args) { + case key == "--index-url": + if hasEq { + indexURL = strings.TrimSpace(val) + } else if i+1 >= len(rest) { + return fmt.Errorf("--index-url requires a value") + } else { + i++ + indexURL = strings.TrimSpace(rest[i]) + } + case key == "--limit" || key == "-n": + if hasEq { + n, err := strconv.Atoi(strings.TrimSpace(val)) + if err != nil || n < 1 { + return fmt.Errorf("--limit must be a positive number") + } + limit = n + } else if i+1 >= len(rest) { return fmt.Errorf("--limit requires a value") + } else { + i++ + n, err := strconv.Atoi(rest[i]) + if err != nil || n < 1 { + return fmt.Errorf("--limit must be a positive number") + } + limit = n } - i++ - n, err := strconv.Atoi(args[i]) - if err != nil || n < 1 { - return fmt.Errorf("--limit must be a positive number") - } - limit = n - case arg == "--help" || arg == "-h": + case key == "--help" || key == "-h": printSearchHelp() return nil case strings.HasPrefix(arg, "-"): @@ -81,14 +99,14 @@ func cmdSearch(args []string) error { // JSON mode: silent search, output JSON if jsonOutput { - return searchJSON(query, limit) + return searchJSON(query, limit, indexURL) } // Interactive mode - return searchInteractive(query, limit, listOnly, mode, cwd) + return searchInteractive(query, limit, listOnly, indexURL, mode, cwd) } -func searchJSON(query string, limit int) error { +func searchJSON(query string, limit int, indexURL string) error { // Show progress on stderr (so JSON output stays clean on stdout) if query == "" { fmt.Fprintf(os.Stderr, "Browsing popular skills...\n") @@ -96,7 +114,13 @@ func searchJSON(query string, limit int) error { fmt.Fprintf(os.Stderr, "Searching for '%s'...\n", query) } - results, err := search.Search(query, limit) + var results []search.SearchResult + var err error + if indexURL != "" { + results, err = search.SearchFromIndexURL(query, limit, indexURL) + } else { + results, err = search.Search(query, limit) + } if err != nil { // Return error as JSON errJSON, _ := json.Marshal(map[string]string{"error": err.Error()}) @@ -114,7 +138,7 @@ func searchJSON(query string, limit int) error { return nil } -func searchInteractive(query string, limit int, listOnly bool, mode runMode, cwd string) error { +func searchInteractive(query string, limit int, listOnly bool, indexURL string, mode runMode, cwd string) error { // Show logo ui.Logo(appversion.Version) @@ -129,14 +153,14 @@ func searchInteractive(query string, limit int, listOnly bool, mode runMode, cwd // List-only mode: single search and exit if listOnly { - _, err := doSearch(query, limit, true, mode, cwd) + _, err := doSearch(query, limit, true, indexURL, mode, cwd) return err } // Interactive loop mode currentQuery := query for { - searchAgain, err := doSearch(currentQuery, limit, false, mode, cwd) + searchAgain, err := doSearch(currentQuery, limit, false, indexURL, mode, cwd) if err != nil { return err } @@ -161,43 +185,58 @@ func searchInteractive(query string, limit int, listOnly bool, mode runMode, cwd } // doSearch performs a search and returns (searchAgain, error) -func doSearch(query string, limit int, listOnly bool, mode runMode, cwd string) (bool, error) { +func doSearch(query string, limit int, listOnly bool, indexURL string, mode runMode, cwd string) (bool, error) { if query == "" { ui.StepStart("Browsing", "popular skills") } else { ui.StepStart("Searching", query) } - spinner := ui.StartTreeSpinner("Querying GitHub...", false) + var spinnerMsg string + if indexURL != "" { + spinnerMsg = "Querying index..." + } else { + spinnerMsg = "Querying GitHub..." + } + spinner := ui.StartTreeSpinner(spinnerMsg, false) - results, err := search.Search(query, limit) + var results []search.SearchResult + var err error + if indexURL != "" { + results, err = search.SearchFromIndexURL(query, limit, indexURL) + } else { + results, err = search.Search(query, limit) + } if err != nil { spinner.Fail("Search failed") - // Handle authentication required error - if _, ok := err.(*search.AuthRequiredError); ok { - fmt.Println() - ui.Warning("GitHub Code Search API requires authentication") - fmt.Println() - ui.Info("Option 1: Login with GitHub CLI (recommended)") - fmt.Printf(" %sgh auth login%s\n", ui.Gray, ui.Reset) - fmt.Println() - ui.Info("Option 2: Set GITHUB_TOKEN environment variable") - fmt.Printf(" %sexport GITHUB_TOKEN=ghp_your_token_here%s\n", ui.Gray, ui.Reset) - return false, nil - } + // GitHub-specific errors only apply when not using index + if indexURL == "" { + // Handle authentication required error + if _, ok := err.(*search.AuthRequiredError); ok { + fmt.Println() + ui.Warning("GitHub Code Search API requires authentication") + fmt.Println() + ui.Info("Option 1: Login with GitHub CLI (recommended)") + fmt.Printf(" %sgh auth login%s\n", ui.Gray, ui.Reset) + fmt.Println() + ui.Info("Option 2: Set GITHUB_TOKEN environment variable") + fmt.Printf(" %sexport GITHUB_TOKEN=ghp_your_token_here%s\n", ui.Gray, ui.Reset) + return false, nil + } - // Handle rate limit error with helpful message - if rateLimitErr, ok := err.(*search.RateLimitError); ok { - fmt.Println() - ui.Warning("GitHub API rate limit exceeded") - if rateLimitErr.Remaining == "0" { - ui.Info("Limit: %s requests/hour", rateLimitErr.Limit) + // Handle rate limit error with helpful message + if rateLimitErr, ok := err.(*search.RateLimitError); ok { + fmt.Println() + ui.Warning("GitHub API rate limit exceeded") + if rateLimitErr.Remaining == "0" { + ui.Info("Limit: %s requests/hour", rateLimitErr.Limit) + } + fmt.Println() + ui.Info("To increase rate limit, set GITHUB_TOKEN:") + fmt.Printf(" %sexport GITHUB_TOKEN=ghp_your_token_here%s\n", ui.Gray, ui.Reset) + return false, nil } - fmt.Println() - ui.Info("To increase rate limit, set GITHUB_TOKEN:") - fmt.Printf(" %sexport GITHUB_TOKEN=ghp_your_token_here%s\n", ui.Gray, ui.Reset) - return false, nil } return false, err } @@ -502,12 +541,13 @@ Search GitHub for skills containing SKILL.md files. When no query is provided, browses popular skills. Options: - --project, -p Install to project-level config (.skillshare/) - --global, -g Install to global config (~/.config/skillshare) - --json Output results as JSON - --list, -l List results only (no install prompt) - --limit N, -n Maximum results (default: 20, max: 100) - --help, -h Show this help + --project, -p Install to project-level config (.skillshare/) + --global, -g Install to global config (~/.config/skillshare) + --index-url URL Search from a private index (local path or HTTP URL) + --json Output results as JSON + --list, -l List results only (no install prompt) + --limit N, -n Maximum results (default: 20, max: 100) + --help, -h Show this help Examples: skillshare search Browse popular skills @@ -516,5 +556,9 @@ Examples: skillshare search commit --limit 10 skillshare search frontend --json skillshare search react --list - skillshare search pdf -p`) + skillshare search pdf -p + + # Private index search + skillshare search --index-url /path/to/index.json + skillshare search react --index-url https://internal.corp/skills/index.json`) } diff --git a/internal/search/index.go b/internal/search/index.go new file mode 100644 index 000000000..7c2b1ed5e --- /dev/null +++ b/internal/search/index.go @@ -0,0 +1,157 @@ +package search + +import ( + "encoding/json" + "fmt" + "net/http" + "os" + "path/filepath" + "sort" + "strings" + "time" +) + +type indexDocument struct { + SourcePath string `json:"sourcePath"` + Skills []indexSkill `json:"skills"` +} + +type indexSkill struct { + Name string `json:"name"` + Description string `json:"description"` + Source string `json:"source"` +} + +// SearchFromIndexURL searches skills from a private index.json URL or local path. +func SearchFromIndexURL(query string, limit int, indexURL string) ([]SearchResult, error) { + limit = normalizeLimit(limit) + doc, err := loadIndex(indexURL) + if err != nil { + return nil, err + } + + sourcePath := strings.TrimSpace(doc.SourcePath) + + q := strings.ToLower(strings.TrimSpace(query)) + results := make([]SearchResult, 0, len(doc.Skills)) + for _, it := range doc.Skills { + name := strings.TrimSpace(it.Name) + source := strings.TrimSpace(it.Source) + if name == "" { + continue + } + if source == "" { + source = name + } + + // Resolve relative source paths using sourcePath from the index. + // A relative source (e.g. "team/skill") would otherwise be misinterpreted + // as a GitHub shorthand. Joining with sourcePath produces an absolute + // local path that ParseSource handles correctly. + if sourcePath != "" && isRelativeSource(source) { + source = filepath.Join(sourcePath, source) + } + + if q != "" { + hay := strings.ToLower(name + "\n" + it.Description + "\n" + source) + if !strings.Contains(hay, q) { + continue + } + } + owner, repo := parseOwnerRepo(source) + results = append(results, SearchResult{ + Name: name, + Description: strings.TrimSpace(it.Description), + Source: source, + Owner: owner, + Repo: repo, + }) + } + + sort.Slice(results, func(i, j int) bool { + return results[i].Name < results[j].Name + }) + if len(results) > limit { + results = results[:limit] + } + return results, nil +} + +// isRelativeSource returns true if the source looks like a relative path +// rather than a remote URL or absolute path. +func isRelativeSource(source string) bool { + if strings.HasPrefix(source, "/") || + strings.HasPrefix(source, "~") || + strings.HasPrefix(source, "git@") || + strings.HasPrefix(source, "http://") || + strings.HasPrefix(source, "https://") || + strings.HasPrefix(source, "file://") { + return false + } + // Windows absolute paths: C:\ or C:/ + if len(source) >= 3 && source[1] == ':' && + ((source[0] >= 'A' && source[0] <= 'Z') || (source[0] >= 'a' && source[0] <= 'z')) && + (source[2] == '/' || source[2] == '\\') { + return false + } + // Windows UNC paths: \\server\share + if strings.HasPrefix(source, `\\`) { + return false + } + // If the first path segment contains a dot, it's a domain (e.g. gitlab.com/...) + firstSlash := strings.Index(source, "/") + if firstSlash > 0 && strings.Contains(source[:firstSlash], ".") { + return false + } + return true +} + +func loadIndex(indexURL string) (*indexDocument, error) { + s := strings.TrimSpace(indexURL) + if s == "" { + return nil, fmt.Errorf("index URL is required") + } + + if strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://") { + client := &http.Client{Timeout: 15 * time.Second} + req, err := http.NewRequest("GET", s, nil) + if err != nil { + return nil, err + } + resp, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("fetch index: %w", err) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("fetch index: HTTP %d", resp.StatusCode) + } + var doc indexDocument + if err := json.NewDecoder(resp.Body).Decode(&doc); err != nil { + return nil, fmt.Errorf("parse index: %w", err) + } + return &doc, nil + } + + rawPath := strings.TrimPrefix(s, "file://") + data, err := os.ReadFile(rawPath) + if err != nil { + return nil, err + } + var doc indexDocument + if err := json.Unmarshal(data, &doc); err != nil { + return nil, fmt.Errorf("parse index: %w", err) + } + return &doc, nil +} + +func parseOwnerRepo(source string) (owner, repo string) { + s := strings.TrimPrefix(source, "https://") + s = strings.TrimPrefix(s, "http://") + s = strings.TrimPrefix(s, "github.com/") + parts := strings.Split(s, "/") + if len(parts) >= 2 { + return parts[0], parts[1] + } + return "", "" +} diff --git a/internal/search/index_test.go b/internal/search/index_test.go new file mode 100644 index 000000000..047669e14 --- /dev/null +++ b/internal/search/index_test.go @@ -0,0 +1,203 @@ +package search + +import ( + "os" + "path/filepath" + "testing" +) + +func writeTestIndex(t *testing.T, dir, filename, content string) string { + t.Helper() + p := filepath.Join(dir, filename) + if err := os.WriteFile(p, []byte(content), 0644); err != nil { + t.Fatalf("write %s: %v", filename, err) + } + return p +} + +func TestSearchFromIndexURL_LocalFile(t *testing.T) { + dir := t.TempDir() + indexPath := writeTestIndex(t, dir, "index.json", `{ + "schemaVersion": 1, + "skills": [ + {"name": "react-patterns", "description": "React perf", "source": "facebook/react/.claude/skills/react-patterns"}, + {"name": "deploy-helper", "description": "K8s deploy", "source": "gitlab.com/ops/skills/deploy-helper"} + ] + }`) + + results, err := SearchFromIndexURL("react", 20, indexPath) + if err != nil { + t.Fatalf("SearchFromIndexURL: %v", err) + } + if len(results) != 1 { + t.Fatalf("got %d results, want 1", len(results)) + } + if results[0].Name != "react-patterns" { + t.Errorf("name = %q, want react-patterns", results[0].Name) + } +} + +func TestSearchFromIndexURL_EmptyQuery(t *testing.T) { + dir := t.TempDir() + indexPath := writeTestIndex(t, dir, "index.json", `{ + "schemaVersion": 1, + "skills": [ + {"name": "alpha", "source": "a/b"}, + {"name": "beta", "source": "c/d"}, + {"name": "gamma", "source": "e/f"} + ] + }`) + + results, err := SearchFromIndexURL("", 20, indexPath) + if err != nil { + t.Fatalf("SearchFromIndexURL: %v", err) + } + if len(results) != 3 { + t.Fatalf("got %d results, want 3 (all skills)", len(results)) + } +} + +func TestSearchFromIndexURL_MalformedJSON(t *testing.T) { + dir := t.TempDir() + indexPath := writeTestIndex(t, dir, "index.json", `{not valid json}`) + + _, err := SearchFromIndexURL("test", 20, indexPath) + if err == nil { + t.Fatal("expected error for malformed JSON") + } +} + +func TestSearchFromIndexURL_LimitRespected(t *testing.T) { + dir := t.TempDir() + indexPath := writeTestIndex(t, dir, "index.json", `{ + "schemaVersion": 1, + "skills": [ + {"name": "a", "source": "x/a"}, + {"name": "b", "source": "x/b"}, + {"name": "c", "source": "x/c"}, + {"name": "d", "source": "x/d"}, + {"name": "e", "source": "x/e"} + ] + }`) + + results, err := SearchFromIndexURL("", 2, indexPath) + if err != nil { + t.Fatalf("SearchFromIndexURL: %v", err) + } + if len(results) != 2 { + t.Fatalf("got %d results, want 2 (limited)", len(results)) + } +} + +func TestSearchFromIndexURL_SourcePathJoin(t *testing.T) { + dir := t.TempDir() + indexPath := writeTestIndex(t, dir, "index.json", `{ + "schemaVersion": 1, + "sourcePath": "/home/user/skills", + "skills": [ + {"name": "my-skill", "source": "_team/frontend/my-skill"} + ] + }`) + + results, err := SearchFromIndexURL("", 20, indexPath) + if err != nil { + t.Fatalf("SearchFromIndexURL: %v", err) + } + if len(results) != 1 { + t.Fatalf("got %d results, want 1", len(results)) + } + want := filepath.Join("/home/user/skills", "_team/frontend/my-skill") + if results[0].Source != want { + t.Errorf("source = %q, want %q", results[0].Source, want) + } +} + +func TestSearchFromIndexURL_NoSourcePath(t *testing.T) { + dir := t.TempDir() + indexPath := writeTestIndex(t, dir, "index.json", `{ + "schemaVersion": 1, + "skills": [ + {"name": "remote", "source": "github.com/owner/repo/skill"} + ] + }`) + + results, err := SearchFromIndexURL("", 20, indexPath) + if err != nil { + t.Fatalf("SearchFromIndexURL: %v", err) + } + if len(results) != 1 { + t.Fatalf("got %d results, want 1", len(results)) + } + if results[0].Source != "github.com/owner/repo/skill" { + t.Errorf("source = %q, want original", results[0].Source) + } +} + +func TestSearchFromIndexURL_EmptyURL(t *testing.T) { + _, err := SearchFromIndexURL("test", 20, "") + if err == nil { + t.Fatal("expected error for empty URL") + } +} + +func TestSearchFromIndexURL_SkipsEmptyName(t *testing.T) { + dir := t.TempDir() + indexPath := writeTestIndex(t, dir, "index.json", `{ + "schemaVersion": 1, + "skills": [ + {"name": "", "source": "x/y"}, + {"name": "valid", "source": "a/b"} + ] + }`) + + results, err := SearchFromIndexURL("", 20, indexPath) + if err != nil { + t.Fatalf("SearchFromIndexURL: %v", err) + } + if len(results) != 1 { + t.Fatalf("got %d results, want 1 (skip empty name)", len(results)) + } +} + +func TestIsRelativeSource(t *testing.T) { + tests := []struct { + source string + want bool + }{ + // Relative paths (should return true) + {"_team/frontend/skill", true}, + {"my-skill", true}, + {"subdir/skill", true}, + {"owner/repo/path", true}, // No dot in first segment → GitHub shorthand, but still "relative" in index context + + // Absolute paths (should return false) + {"/shared/nfs/skills/x", false}, + {"~/skills/x", false}, + + // Remote URLs (should return false) + {"git@gitlab.com:team/repo.git//x", false}, + {"http://example.com/index.json", false}, + {"https://gitlab.com/team/repo/x", false}, + {"file:///path/x", false}, + + // Domain detection (should return false) + {"gitlab.com/team/repo/x", false}, + {"bitbucket.org/team/repo/x", false}, + {"gitea.company.com/team/repo/x", false}, + {"github.com/owner/repo/skill", false}, + + // Windows paths (should return false) + {`C:\skills\foo`, false}, + {`D:/projects/skill`, false}, + {`\\server\share\skills\foo`, false}, + } + + for _, tt := range tests { + t.Run(tt.source, func(t *testing.T) { + got := isRelativeSource(tt.source) + if got != tt.want { + t.Errorf("isRelativeSource(%q) = %v, want %v", tt.source, got, tt.want) + } + }) + } +}