* chore(io): Migrate pkg/repos to Medium abstraction - Modified Registry and Repo structs in pkg/repos/registry.go to include io.Medium. - Updated LoadRegistry, FindRegistry, and ScanDirectory signatures to accept io.Medium. - Migrated all internal file operations in pkg/repos/registry.go to use the Medium interface instead of io.Local or os package. - Updated dozens of call sites across internal/cmd/ to pass io.Local to the updated repos functions. - Ensured consistent use of io.Medium for repo existence and git checks. * chore(io): Fix undefined io errors in repos migration - Fixed "undefined: io" compilation errors by using the correct 'coreio' alias in internal commands. - Corrected FindRegistry and LoadRegistry calls in cmd_file_sync.go, cmd_install.go, and cmd_search.go. - Verified fix with successful project-wide build. * chore(io): Final fixes for repos Medium migration - Fixed formatting issue in internal/cmd/setup/cmd_github.go by using 'coreio' alias for consistency. - Ensured all callers use the 'coreio' alias when referring to the io package. - Verified project-wide build completes successfully. * chore(io): Complete migration of pkg/repos to io.Medium - Migrated pkg/repos/registry.go to use io.Medium abstraction for all file operations. - Updated all callers in internal/cmd/ to pass io.Local, with proper alias handling. - Fixed formatting issues in cmd_github.go that caused previous CI failures. - Added unit tests in pkg/repos/registry_test.go using io.MockMedium. - Verified project-wide build and new unit tests pass. * chore(io): Address PR feedback for Medium migration - Made pkg/repos truly medium-agnostic by removing local filepath.Abs calls. - Restored Medium abstraction in pkg/cli/daemon.go (PIDFile and Daemon). - Restored context cancellation checks in pkg/container/linuxkit.go. - Updated pkg/cli/daemon_test.go to use MockMedium. - Documented FindRegistry's local filesystem dependencies. - Verified project-wide build and tests pass. * chore(io): Fix merge conflicts and address PR feedback - Resolved merge conflicts with latest dev branch. - Restored Medium abstraction in pkg/cli/daemon.go and context checks in pkg/container/linuxkit.go. - Refactored pkg/repos/registry.go to be truly medium-agnostic (removed filepath.Abs). - Updated pkg/cli/daemon_test.go to use MockMedium. - Verified all builds and tests pass locally. * chore(io): Complete pkg/repos Medium migration and PR feedback - Refactored pkg/repos to use io.Medium abstraction, removing local filesystem dependencies. - Updated all call sites in internal/cmd to pass io.Local/coreio.Local. - Restored Medium abstraction in pkg/cli/daemon.go and context checks in pkg/container/linuxkit.go. - Updated pkg/cli/daemon_test.go to use MockMedium for better test isolation. - Fixed merge conflicts and code formatting issues. - Verified project-wide build and tests pass. * fix(lint): handle error return values in registry tests Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <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"
|
|
|
|
"github.com/host-uk/core/pkg/cache"
|
|
"github.com/host-uk/core/pkg/i18n"
|
|
coreio "github.com/host-uk/core/pkg/io"
|
|
"github.com/host-uk/core/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(nil, 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
|
|
}
|