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.
This commit is contained in:
Snider 2026-02-04 14:27:06 +00:00
parent 26d7c80b1e
commit 04949a21f1
18 changed files with 88 additions and 65 deletions

View file

@ -225,12 +225,12 @@ func runApply() error {
// getApplyTargetRepos gets repos to apply command to
func getApplyTargetRepos() ([]*repos.Repo, error) {
// Load registry
registryPath, err := repos.FindRegistry()
registryPath, err := repos.FindRegistry(io.Local)
if err != nil {
return nil, core.E("dev.apply", "failed to find registry", err)
}
registry, err := repos.LoadRegistry(registryPath)
registry, err := repos.LoadRegistry(io.Local, registryPath)
if err != nil {
return nil, core.E("dev.apply", "failed to load registry", err)
}

View file

@ -10,6 +10,7 @@ import (
"github.com/host-uk/core/pkg/cli"
"github.com/host-uk/core/pkg/i18n"
"github.com/host-uk/core/pkg/io"
"github.com/host-uk/core/pkg/repos"
)
@ -75,20 +76,20 @@ func runCI(registryPath string, branch string, failedOnly bool) error {
var err error
if registryPath != "" {
reg, err = repos.LoadRegistry(registryPath)
reg, err = repos.LoadRegistry(io.Local, registryPath)
if err != nil {
return cli.Wrap(err, "failed to load registry")
}
} else {
registryPath, err = repos.FindRegistry()
registryPath, err = repos.FindRegistry(io.Local)
if err == nil {
reg, err = repos.LoadRegistry(registryPath)
reg, err = repos.LoadRegistry(io.Local, registryPath)
if err != nil {
return cli.Wrap(err, "failed to load registry")
}
} else {
cwd, _ := os.Getwd()
reg, err = repos.ScanDirectory(cwd)
reg, err = repos.ScanDirectory(io.Local, cwd)
if err != nil {
return cli.Wrap(err, "failed to scan directory")
}

View file

@ -195,12 +195,12 @@ func runFileSync(source string) error {
// resolveTargetRepos resolves the --to pattern to actual repos
func resolveTargetRepos(pattern string) ([]*repos.Repo, error) {
// Load registry
registryPath, err := repos.FindRegistry()
registryPath, err := repos.FindRegistry(io.Local)
if err != nil {
return nil, log.E("dev.sync", "failed to find registry", err)
}
registry, err := repos.LoadRegistry(registryPath)
registry, err := repos.LoadRegistry(io.Local, registryPath)
if err != nil {
return nil, log.E("dev.sync", "failed to load registry", err)
}

View file

@ -6,6 +6,7 @@ import (
"github.com/host-uk/core/pkg/cli"
"github.com/host-uk/core/pkg/i18n"
"github.com/host-uk/core/pkg/io"
"github.com/host-uk/core/pkg/repos"
)
@ -42,14 +43,14 @@ func runImpact(registryPath string, repoName string) error {
var err error
if registryPath != "" {
reg, err = repos.LoadRegistry(registryPath)
reg, err = repos.LoadRegistry(io.Local, registryPath)
if err != nil {
return cli.Wrap(err, "failed to load registry")
}
} else {
registryPath, err = repos.FindRegistry()
registryPath, err = repos.FindRegistry(io.Local)
if err == nil {
reg, err = repos.LoadRegistry(registryPath)
reg, err = repos.LoadRegistry(io.Local, registryPath)
if err != nil {
return cli.Wrap(err, "failed to load registry")
}

View file

@ -8,6 +8,7 @@ import (
"github.com/host-uk/core/internal/cmd/workspace"
"github.com/host-uk/core/pkg/cli"
"github.com/host-uk/core/pkg/i18n"
"github.com/host-uk/core/pkg/io"
"github.com/host-uk/core/pkg/repos"
)
@ -18,16 +19,16 @@ func loadRegistryWithConfig(registryPath string) (*repos.Registry, string, error
var registryDir string
if registryPath != "" {
reg, err = repos.LoadRegistry(registryPath)
reg, err = repos.LoadRegistry(io.Local, registryPath)
if err != nil {
return nil, "", cli.Wrap(err, "failed to load registry")
}
cli.Print("%s %s\n\n", dimStyle.Render(i18n.Label("registry")), registryPath)
registryDir = filepath.Dir(registryPath)
} else {
registryPath, err = repos.FindRegistry()
registryPath, err = repos.FindRegistry(io.Local)
if err == nil {
reg, err = repos.LoadRegistry(registryPath)
reg, err = repos.LoadRegistry(io.Local, registryPath)
if err != nil {
return nil, "", cli.Wrap(err, "failed to load registry")
}
@ -36,7 +37,7 @@ func loadRegistryWithConfig(registryPath string) (*repos.Registry, string, error
} else {
// Fallback: scan current directory
cwd, _ := os.Getwd()
reg, err = repos.ScanDirectory(cwd)
reg, err = repos.ScanDirectory(io.Local, cwd)
if err != nil {
return nil, "", cli.Wrap(err, "failed to scan directory")
}

View file

@ -30,22 +30,22 @@ func loadRegistry(registryPath string) (*repos.Registry, string, error) {
var registryDir string
if registryPath != "" {
reg, err = repos.LoadRegistry(registryPath)
reg, err = repos.LoadRegistry(io.Local, registryPath)
if err != nil {
return nil, "", cli.Wrap(err, i18n.T("i18n.fail.load", "registry"))
}
registryDir = filepath.Dir(registryPath)
} else {
registryPath, err = repos.FindRegistry()
registryPath, err = repos.FindRegistry(io.Local)
if err == nil {
reg, err = repos.LoadRegistry(registryPath)
reg, err = repos.LoadRegistry(io.Local, registryPath)
if err != nil {
return nil, "", cli.Wrap(err, i18n.T("i18n.fail.load", "registry"))
}
registryDir = filepath.Dir(registryPath)
} else {
cwd, _ := os.Getwd()
reg, err = repos.ScanDirectory(cwd)
reg, err = repos.ScanDirectory(io.Local, cwd)
if err != nil {
return nil, "", cli.Wrap(err, i18n.T("i18n.fail.scan", "directory"))
}

View file

@ -8,6 +8,7 @@ import (
"strings"
"github.com/host-uk/core/pkg/i18n"
"github.com/host-uk/core/pkg/io"
"github.com/host-uk/core/pkg/repos"
)
@ -43,11 +44,11 @@ func checkGitHubCLI() bool {
// checkWorkspace checks for repos.yaml and counts cloned repos
func checkWorkspace() {
registryPath, err := repos.FindRegistry()
registryPath, err := repos.FindRegistry(io.Local)
if err == nil {
fmt.Printf(" %s %s\n", successStyle.Render("✓"), i18n.T("cmd.doctor.repos_yaml_found", map[string]interface{}{"Path": registryPath}))
reg, err := repos.LoadRegistry(registryPath)
reg, err := repos.LoadRegistry(io.Local, registryPath)
if err == nil {
basePath := reg.BasePath
if basePath == "" {

View file

@ -18,6 +18,7 @@ import (
"github.com/host-uk/core/pkg/cli"
"github.com/host-uk/core/pkg/i18n"
"github.com/host-uk/core/pkg/io"
"github.com/host-uk/core/pkg/log"
"github.com/host-uk/core/pkg/repos"
)
@ -177,12 +178,12 @@ func resolveRepos() ([]string, error) {
if monitorAll {
// All repos from registry
registry, err := repos.FindRegistry()
registry, err := repos.FindRegistry(io.Local)
if err != nil {
return nil, log.E("monitor", "failed to find registry", err)
}
loaded, err := repos.LoadRegistry(registry)
loaded, err := repos.LoadRegistry(io.Local, registry)
if err != nil {
return nil, log.E("monitor", "failed to load registry", err)
}

View file

@ -51,8 +51,8 @@ func runPkgInstall(repoArg, targetDir string, addToRegistry bool) error {
// Determine target directory
if targetDir == "" {
if regPath, err := repos.FindRegistry(); err == nil {
if reg, err := repos.LoadRegistry(regPath); err == nil {
if regPath, err := repos.FindRegistry(io.Local); err == nil {
if reg, err := repos.LoadRegistry(io.Local, regPath); err == nil {
targetDir = reg.BasePath
if targetDir == "" {
targetDir = "./packages"
@ -110,12 +110,12 @@ func runPkgInstall(repoArg, targetDir string, addToRegistry bool) error {
}
func addToRegistryFile(org, repoName string) error {
regPath, err := repos.FindRegistry()
regPath, err := repos.FindRegistry(io.Local)
if err != nil {
return errors.New(i18n.T("cmd.pkg.error.no_repos_yaml"))
}
reg, err := repos.LoadRegistry(regPath)
reg, err := repos.LoadRegistry(io.Local, regPath)
if err != nil {
return err
}

View file

@ -28,12 +28,12 @@ func addPkgListCommand(parent *cobra.Command) {
}
func runPkgList() error {
regPath, err := repos.FindRegistry()
regPath, err := repos.FindRegistry(coreio.Local)
if err != nil {
return errors.New(i18n.T("cmd.pkg.error.no_repos_yaml_workspace"))
}
reg, err := repos.LoadRegistry(regPath)
reg, err := repos.LoadRegistry(coreio.Local, regPath)
if err != nil {
return fmt.Errorf("%s: %w", i18n.T("i18n.fail.load", "registry"), err)
}
@ -113,12 +113,12 @@ func addPkgUpdateCommand(parent *cobra.Command) {
}
func runPkgUpdate(packages []string, all bool) error {
regPath, err := repos.FindRegistry()
regPath, err := repos.FindRegistry(coreio.Local)
if err != nil {
return errors.New(i18n.T("cmd.pkg.error.no_repos_yaml"))
}
reg, err := repos.LoadRegistry(regPath)
reg, err := repos.LoadRegistry(coreio.Local, regPath)
if err != nil {
return fmt.Errorf("%s: %w", i18n.T("i18n.fail.load", "registry"), err)
}
@ -193,12 +193,12 @@ func addPkgOutdatedCommand(parent *cobra.Command) {
}
func runPkgOutdated() error {
regPath, err := repos.FindRegistry()
regPath, err := repos.FindRegistry(coreio.Local)
if err != nil {
return errors.New(i18n.T("cmd.pkg.error.no_repos_yaml"))
}
reg, err := repos.LoadRegistry(regPath)
reg, err := repos.LoadRegistry(coreio.Local, regPath)
if err != nil {
return fmt.Errorf("%s: %w", i18n.T("i18n.fail.load", "registry"), err)
}

View file

@ -69,7 +69,7 @@ type ghRepo struct {
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(); err == nil {
if regPath, err := repos.FindRegistry(io.Local); err == nil {
cacheDir = filepath.Join(filepath.Dir(regPath), ".core", "cache")
}

View file

@ -14,6 +14,7 @@ import (
"github.com/host-uk/core/pkg/cli"
"github.com/host-uk/core/pkg/i18n"
"github.com/host-uk/core/pkg/io"
"github.com/host-uk/core/pkg/log"
"github.com/host-uk/core/pkg/repos"
)
@ -71,13 +72,13 @@ func runHealth() error {
var err error
if healthRegistry != "" {
reg, err = repos.LoadRegistry(healthRegistry)
reg, err = repos.LoadRegistry(io.Local, healthRegistry)
} else {
registryPath, findErr := repos.FindRegistry()
registryPath, findErr := repos.FindRegistry(io.Local)
if findErr != nil {
return log.E("qa.health", i18n.T("error.registry_not_found"), nil)
}
reg, err = repos.LoadRegistry(registryPath)
reg, err = repos.LoadRegistry(io.Local, registryPath)
}
if err != nil {
return log.E("qa.health", "failed to load registry", err)

View file

@ -17,6 +17,7 @@ import (
"github.com/host-uk/core/pkg/cli"
"github.com/host-uk/core/pkg/i18n"
"github.com/host-uk/core/pkg/io"
"github.com/host-uk/core/pkg/log"
"github.com/host-uk/core/pkg/repos"
)
@ -100,13 +101,13 @@ func runQAIssues() error {
var err error
if issuesRegistry != "" {
reg, err = repos.LoadRegistry(issuesRegistry)
reg, err = repos.LoadRegistry(io.Local, issuesRegistry)
} else {
registryPath, findErr := repos.FindRegistry()
registryPath, findErr := repos.FindRegistry(io.Local)
if findErr != nil {
return log.E("qa.issues", i18n.T("error.registry_not_found"), nil)
}
reg, err = repos.LoadRegistry(registryPath)
reg, err = repos.LoadRegistry(io.Local, registryPath)
}
if err != nil {
return log.E("qa.issues", "failed to load registry", err)

View file

@ -8,6 +8,7 @@ import (
"github.com/host-uk/core/pkg/cli"
"github.com/host-uk/core/pkg/i18n"
"github.com/host-uk/core/pkg/io"
"github.com/host-uk/core/pkg/repos"
)
@ -106,18 +107,18 @@ type SecretScanningAlert struct {
// loadRegistry loads the repository registry.
func loadRegistry(registryPath string) (*repos.Registry, error) {
if registryPath != "" {
reg, err := repos.LoadRegistry(registryPath)
reg, err := repos.LoadRegistry(io.Local, registryPath)
if err != nil {
return nil, cli.Wrap(err, "load registry")
}
return reg, nil
}
path, err := repos.FindRegistry()
path, err := repos.FindRegistry(io.Local)
if err != nil {
return nil, cli.Wrap(err, "find registry")
}
reg, err := repos.LoadRegistry(path)
reg, err := repos.LoadRegistry(io.Local, path)
if err != nil {
return nil, cli.Wrap(err, "load registry")
}

View file

@ -30,7 +30,7 @@ func runSetupOrchestrator(registryPath, only string, dryRun, all bool, projectNa
if registryPath != "" {
foundRegistry = registryPath
} else {
foundRegistry, err = repos.FindRegistry()
foundRegistry, err = repos.FindRegistry(coreio.Local)
}
// If registry exists, use registry mode
@ -128,7 +128,7 @@ func runBootstrap(ctx context.Context, only string, dryRun, all bool, projectNam
return nil
}
reg, err := repos.LoadRegistry(registryPath)
reg, err := repos.LoadRegistry(coreio.Local, registryPath)
if err != nil {
return fmt.Errorf("failed to load registry from %s: %w", devopsRepo, err)
}

View file

@ -23,6 +23,7 @@ import (
"path/filepath"
"github.com/host-uk/core/pkg/cli"
"github.com/host-uk/core/pkg/io"
"github.com/host-uk/core/pkg/i18n"
"github.com/host-uk/core/pkg/repos"
"github.com/spf13/cobra"
@ -78,12 +79,12 @@ func runGitHubSetup() error {
}
// Find registry
registryPath, err := repos.FindRegistry()
registryPath, err := repos.FindRegistry(io.Local)
if err != nil {
return cli.Wrap(err, i18n.T("error.registry_not_found"))
}
reg, err := repos.LoadRegistry(registryPath)
reg, err := repos.LoadRegistry(io.Local, registryPath)
if err != nil {
return cli.Wrap(err, "failed to load registry")
}

View file

@ -22,7 +22,7 @@ import (
// runRegistrySetup loads a registry from path and runs setup.
func runRegistrySetup(ctx context.Context, registryPath, only string, dryRun, all, runBuild bool) error {
reg, err := repos.LoadRegistry(registryPath)
reg, err := repos.LoadRegistry(coreio.Local, registryPath)
if err != nil {
return fmt.Errorf("failed to load registry: %w", err)
}

View file

@ -20,6 +20,7 @@ type Registry struct {
BasePath string `yaml:"base_path"`
Repos map[string]*Repo `yaml:"repos"`
Defaults RegistryDefaults `yaml:"defaults"`
medium io.Medium `yaml:"-"`
}
// RegistryDefaults contains default values applied to all repos.
@ -56,17 +57,18 @@ type Repo struct {
Clone *bool `yaml:"clone,omitempty"` // nil = true, false = skip cloning
// Computed fields
Path string `yaml:"-"` // Full path to repo directory
Path string `yaml:"-"` // Full path to repo directory
registry *Registry `yaml:"-"`
}
// LoadRegistry reads and parses a repos.yaml file.
func LoadRegistry(path string) (*Registry, error) {
func LoadRegistry(m io.Medium, path string) (*Registry, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("failed to resolve path: %w", err)
}
content, err := io.Local.Read(absPath)
content, err := m.Read(absPath)
if err != nil {
return nil, fmt.Errorf("failed to read registry file: %w", err)
}
@ -77,6 +79,8 @@ func LoadRegistry(path string) (*Registry, error) {
return nil, fmt.Errorf("failed to parse registry file: %w", err)
}
reg.medium = m
// Expand base path
reg.BasePath = expandPath(reg.BasePath)
@ -84,6 +88,7 @@ func LoadRegistry(path string) (*Registry, error) {
for name, repo := range reg.Repos {
repo.Name = name
repo.Path = filepath.Join(reg.BasePath, name)
repo.registry = &reg
// Apply defaults if not set
if repo.CI == "" {
@ -96,7 +101,7 @@ func LoadRegistry(path string) (*Registry, error) {
// FindRegistry searches for repos.yaml in common locations.
// It checks: current directory, parent directories, and home directory.
func FindRegistry() (string, error) {
func FindRegistry(m io.Medium) (string, error) {
// Check current directory and parents
dir, err := os.Getwd()
if err != nil {
@ -105,7 +110,7 @@ func FindRegistry() (string, error) {
for {
candidate := filepath.Join(dir, "repos.yaml")
if io.Local.Exists(candidate) {
if m.Exists(candidate) {
return candidate, nil
}
@ -128,7 +133,7 @@ func FindRegistry() (string, error) {
}
for _, p := range commonPaths {
if io.Local.Exists(p) {
if m.Exists(p) {
return p, nil
}
}
@ -138,13 +143,13 @@ func FindRegistry() (string, error) {
// ScanDirectory creates a Registry by scanning a directory for git repos.
// This is used as a fallback when no repos.yaml is found.
func ScanDirectory(dir string) (*Registry, error) {
func ScanDirectory(m io.Medium, dir string) (*Registry, error) {
absDir, err := filepath.Abs(dir)
if err != nil {
return nil, fmt.Errorf("failed to resolve directory path: %w", err)
}
entries, err := io.Local.List(absDir)
entries, err := m.List(absDir)
if err != nil {
return nil, fmt.Errorf("failed to read directory: %w", err)
}
@ -153,6 +158,7 @@ func ScanDirectory(dir string) (*Registry, error) {
Version: 1,
BasePath: absDir,
Repos: make(map[string]*Repo),
medium: m,
}
// Try to detect org from git remote
@ -164,21 +170,22 @@ func ScanDirectory(dir string) (*Registry, error) {
repoPath := filepath.Join(absDir, entry.Name())
gitPath := filepath.Join(repoPath, ".git")
if !io.Local.IsDir(gitPath) {
if !m.IsDir(gitPath) {
continue // Not a git repo
}
repo := &Repo{
Name: entry.Name(),
Path: repoPath,
Type: "module", // Default type
Name: entry.Name(),
Path: repoPath,
Type: "module", // Default type
registry: reg,
}
reg.Repos[entry.Name()] = repo
// Try to detect org from first repo's remote
if reg.Org == "" {
reg.Org = detectOrg(repoPath)
reg.Org = detectOrg(m, repoPath)
}
}
@ -186,10 +193,10 @@ func ScanDirectory(dir string) (*Registry, error) {
}
// detectOrg tries to extract the GitHub org from a repo's origin remote.
func detectOrg(repoPath string) string {
func detectOrg(m io.Medium, repoPath string) string {
// Try to read git remote
configPath := filepath.Join(repoPath, ".git", "config")
content, err := io.Local.Read(configPath)
content, err := m.Read(configPath)
if err != nil {
return ""
}
@ -301,13 +308,20 @@ func (r *Registry) TopologicalOrder() ([]*Repo, error) {
// Exists checks if the repo directory exists on disk.
func (repo *Repo) Exists() bool {
return io.Local.IsDir(repo.Path)
return repo.getMedium().IsDir(repo.Path)
}
// IsGitRepo checks if the repo directory contains a .git folder.
func (repo *Repo) IsGitRepo() bool {
gitPath := filepath.Join(repo.Path, ".git")
return io.Local.IsDir(gitPath)
return repo.getMedium().IsDir(gitPath)
}
func (repo *Repo) getMedium() io.Medium {
if repo.registry != nil && repo.registry.medium != nil {
return repo.registry.medium
}
return io.Local
}
// expandPath expands ~ to home directory.