- Change module from forge.lthn.ai/core/go to forge.lthn.ai/core/cli - Remove pkg/ directory (now served from core/go) - Add require + replace for forge.lthn.ai/core/go => ../go - Update go.work to include ../go workspace module - Fix all internal/cmd/* imports: pkg/ refs → forge.lthn.ai/core/go/pkg/ - Rename internal/cmd/sdk package to sdkcmd (avoids conflict with pkg/sdk) - Remove SDK library files from internal/cmd/sdk/ (now in core/go/pkg/sdk/) - Remove duplicate RAG helper functions from internal/cmd/rag/ - Remove stale cmd/core-ide/ (now in core/ide repo) - Update IDE variant to remove core-ide import - Fix test assertion for new module name - Run go mod tidy to sync dependencies core/cli is now a pure CLI application importing core/go for packages. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
205 lines
5.3 KiB
Go
205 lines
5.3 KiB
Go
package pkgcmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"forge.lthn.ai/core/go/pkg/cache"
|
|
"forge.lthn.ai/core/go/pkg/i18n"
|
|
coreio "forge.lthn.ai/core/go/pkg/io"
|
|
"forge.lthn.ai/core/go/pkg/repos"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
searchOrg string
|
|
searchPattern string
|
|
searchType string
|
|
searchLimit int
|
|
searchRefresh bool
|
|
)
|
|
|
|
// addPkgSearchCommand adds the 'pkg search' command.
|
|
func addPkgSearchCommand(parent *cobra.Command) {
|
|
searchCmd := &cobra.Command{
|
|
Use: "search",
|
|
Short: i18n.T("cmd.pkg.search.short"),
|
|
Long: i18n.T("cmd.pkg.search.long"),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
org := searchOrg
|
|
pattern := searchPattern
|
|
limit := searchLimit
|
|
if org == "" {
|
|
org = "host-uk"
|
|
}
|
|
if pattern == "" {
|
|
pattern = "*"
|
|
}
|
|
if limit == 0 {
|
|
limit = 50
|
|
}
|
|
return runPkgSearch(org, pattern, searchType, limit, searchRefresh)
|
|
},
|
|
}
|
|
|
|
searchCmd.Flags().StringVar(&searchOrg, "org", "", i18n.T("cmd.pkg.search.flag.org"))
|
|
searchCmd.Flags().StringVar(&searchPattern, "pattern", "", i18n.T("cmd.pkg.search.flag.pattern"))
|
|
searchCmd.Flags().StringVar(&searchType, "type", "", i18n.T("cmd.pkg.search.flag.type"))
|
|
searchCmd.Flags().IntVar(&searchLimit, "limit", 0, i18n.T("cmd.pkg.search.flag.limit"))
|
|
searchCmd.Flags().BoolVar(&searchRefresh, "refresh", false, i18n.T("cmd.pkg.search.flag.refresh"))
|
|
|
|
parent.AddCommand(searchCmd)
|
|
}
|
|
|
|
type ghRepo struct {
|
|
Name string `json:"name"`
|
|
FullName string `json:"full_name"`
|
|
Description string `json:"description"`
|
|
Visibility string `json:"visibility"`
|
|
UpdatedAt string `json:"updated_at"`
|
|
Language string `json:"language"`
|
|
}
|
|
|
|
func runPkgSearch(org, pattern, repoType string, limit int, refresh bool) error {
|
|
// Initialize cache in workspace .core/ directory
|
|
var cacheDir string
|
|
if regPath, err := repos.FindRegistry(coreio.Local); err == nil {
|
|
cacheDir = filepath.Join(filepath.Dir(regPath), ".core", "cache")
|
|
}
|
|
|
|
c, err := cache.New(cacheDir, 0)
|
|
if err != nil {
|
|
c = nil
|
|
}
|
|
|
|
cacheKey := cache.GitHubReposKey(org)
|
|
var ghRepos []ghRepo
|
|
var fromCache bool
|
|
|
|
// Try cache first (unless refresh requested)
|
|
if c != nil && !refresh {
|
|
if found, err := c.Get(cacheKey, &ghRepos); found && err == nil {
|
|
fromCache = true
|
|
age := c.Age(cacheKey)
|
|
fmt.Printf("%s %s %s\n", dimStyle.Render(i18n.T("cmd.pkg.search.cache_label")), org, dimStyle.Render(fmt.Sprintf("(%s ago)", age.Round(time.Second))))
|
|
}
|
|
}
|
|
|
|
// Fetch from GitHub if not cached
|
|
if !fromCache {
|
|
if !ghAuthenticated() {
|
|
return errors.New(i18n.T("cmd.pkg.error.gh_not_authenticated"))
|
|
}
|
|
|
|
if os.Getenv("GH_TOKEN") != "" {
|
|
fmt.Printf("%s %s\n", dimStyle.Render(i18n.Label("note")), i18n.T("cmd.pkg.search.gh_token_warning"))
|
|
fmt.Printf("%s %s\n\n", dimStyle.Render(""), i18n.T("cmd.pkg.search.gh_token_unset"))
|
|
}
|
|
|
|
fmt.Printf("%s %s... ", dimStyle.Render(i18n.T("cmd.pkg.search.fetching_label")), org)
|
|
|
|
cmd := exec.Command("gh", "repo", "list", org,
|
|
"--json", "name,description,visibility,updatedAt,primaryLanguage",
|
|
"--limit", fmt.Sprintf("%d", limit))
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
fmt.Println()
|
|
errStr := strings.TrimSpace(string(output))
|
|
if strings.Contains(errStr, "401") || strings.Contains(errStr, "Bad credentials") {
|
|
return errors.New(i18n.T("cmd.pkg.error.auth_failed"))
|
|
}
|
|
return fmt.Errorf("%s: %s", i18n.T("cmd.pkg.error.search_failed"), errStr)
|
|
}
|
|
|
|
if err := json.Unmarshal(output, &ghRepos); err != nil {
|
|
return fmt.Errorf("%s: %w", i18n.T("i18n.fail.parse", "results"), err)
|
|
}
|
|
|
|
if c != nil {
|
|
_ = c.Set(cacheKey, ghRepos)
|
|
}
|
|
|
|
fmt.Printf("%s\n", successStyle.Render("✓"))
|
|
}
|
|
|
|
// Filter by glob pattern and type
|
|
var filtered []ghRepo
|
|
for _, r := range ghRepos {
|
|
if !matchGlob(pattern, r.Name) {
|
|
continue
|
|
}
|
|
if repoType != "" && !strings.Contains(r.Name, repoType) {
|
|
continue
|
|
}
|
|
filtered = append(filtered, r)
|
|
}
|
|
|
|
if len(filtered) == 0 {
|
|
fmt.Println(i18n.T("cmd.pkg.search.no_repos_found"))
|
|
return nil
|
|
}
|
|
|
|
sort.Slice(filtered, func(i, j int) bool {
|
|
return filtered[i].Name < filtered[j].Name
|
|
})
|
|
|
|
fmt.Print(i18n.T("cmd.pkg.search.found_repos", map[string]int{"Count": len(filtered)}) + "\n\n")
|
|
|
|
for _, r := range filtered {
|
|
visibility := ""
|
|
if r.Visibility == "private" {
|
|
visibility = dimStyle.Render(" " + i18n.T("cmd.pkg.search.private_label"))
|
|
}
|
|
|
|
desc := r.Description
|
|
if len(desc) > 50 {
|
|
desc = desc[:47] + "..."
|
|
}
|
|
if desc == "" {
|
|
desc = dimStyle.Render(i18n.T("cmd.pkg.no_description"))
|
|
}
|
|
|
|
fmt.Printf(" %s%s\n", repoNameStyle.Render(r.Name), visibility)
|
|
fmt.Printf(" %s\n", desc)
|
|
}
|
|
|
|
fmt.Println()
|
|
fmt.Printf("%s %s\n", i18n.T("common.hint.install_with"), dimStyle.Render(fmt.Sprintf("core pkg install %s/<repo-name>", org)))
|
|
|
|
return nil
|
|
}
|
|
|
|
// matchGlob does simple glob matching with * wildcards
|
|
func matchGlob(pattern, name string) bool {
|
|
if pattern == "*" || pattern == "" {
|
|
return true
|
|
}
|
|
|
|
parts := strings.Split(pattern, "*")
|
|
pos := 0
|
|
for i, part := range parts {
|
|
if part == "" {
|
|
continue
|
|
}
|
|
idx := strings.Index(name[pos:], part)
|
|
if idx == -1 {
|
|
return false
|
|
}
|
|
if i == 0 && !strings.HasPrefix(pattern, "*") && idx != 0 {
|
|
return false
|
|
}
|
|
pos += idx + len(part)
|
|
}
|
|
if !strings.HasSuffix(pattern, "*") && pos != len(name) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|