Merge branch 'fix/io-migration-repos' into new

# Conflicts:
#	pkg/repos/registry.go
This commit is contained in:
Snider 2026-02-08 21:28:50 +00:00
commit 1ac04479f6

View file

@ -20,7 +20,6 @@ type Registry struct {
BasePath string `yaml:"base_path"` BasePath string `yaml:"base_path"`
Repos map[string]*Repo `yaml:"repos"` Repos map[string]*Repo `yaml:"repos"`
Defaults RegistryDefaults `yaml:"defaults"` Defaults RegistryDefaults `yaml:"defaults"`
medium io.Medium `yaml:"-"`
} }
// RegistryDefaults contains default values applied to all repos. // RegistryDefaults contains default values applied to all repos.
@ -57,26 +56,21 @@ type Repo struct {
Clone *bool `yaml:"clone,omitempty"` // nil = true, false = skip cloning Clone *bool `yaml:"clone,omitempty"` // nil = true, false = skip cloning
// Computed fields // 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 from the given medium. // LoadRegistry reads and parses a repos.yaml file.
// The path should be a valid path for the provided medium. func LoadRegistry(path string) (*Registry, error) {
func LoadRegistry(m io.Medium, path string) (*Registry, error) { dataStr, err := io.Local.Read(path)
content, err := m.Read(path)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read registry file: %w", err) return nil, fmt.Errorf("failed to read registry file: %w", err)
} }
data := []byte(content)
var reg Registry var reg Registry
if err := yaml.Unmarshal(data, &reg); err != nil { if err := yaml.Unmarshal([]byte(dataStr), &reg); err != nil {
return nil, fmt.Errorf("failed to parse registry file: %w", err) return nil, fmt.Errorf("failed to parse registry file: %w", err)
} }
reg.medium = m
// Expand base path // Expand base path
reg.BasePath = expandPath(reg.BasePath) reg.BasePath = expandPath(reg.BasePath)
@ -84,7 +78,6 @@ func LoadRegistry(m io.Medium, path string) (*Registry, error) {
for name, repo := range reg.Repos { for name, repo := range reg.Repos {
repo.Name = name repo.Name = name
repo.Path = filepath.Join(reg.BasePath, name) repo.Path = filepath.Join(reg.BasePath, name)
repo.registry = &reg
// Apply defaults if not set // Apply defaults if not set
if repo.CI == "" { if repo.CI == "" {
@ -97,8 +90,7 @@ func LoadRegistry(m io.Medium, path string) (*Registry, error) {
// FindRegistry searches for repos.yaml in common locations. // FindRegistry searches for repos.yaml in common locations.
// It checks: current directory, parent directories, and home directory. // It checks: current directory, parent directories, and home directory.
// This function is primarily intended for use with io.Local or other local-like filesystems. func FindRegistry() (string, error) {
func FindRegistry(m io.Medium) (string, error) {
// Check current directory and parents // Check current directory and parents
dir, err := os.Getwd() dir, err := os.Getwd()
if err != nil { if err != nil {
@ -107,7 +99,7 @@ func FindRegistry(m io.Medium) (string, error) {
for { for {
candidate := filepath.Join(dir, "repos.yaml") candidate := filepath.Join(dir, "repos.yaml")
if m.Exists(candidate) { if io.Local.IsFile(candidate) {
return candidate, nil return candidate, nil
} }
@ -130,7 +122,7 @@ func FindRegistry(m io.Medium) (string, error) {
} }
for _, p := range commonPaths { for _, p := range commonPaths {
if m.Exists(p) { if io.Local.IsFile(p) {
return p, nil return p, nil
} }
} }
@ -140,9 +132,8 @@ func FindRegistry(m io.Medium) (string, error) {
// ScanDirectory creates a Registry by scanning a directory for git repos. // ScanDirectory creates a Registry by scanning a directory for git repos.
// This is used as a fallback when no repos.yaml is found. // This is used as a fallback when no repos.yaml is found.
// The dir should be a valid path for the provided medium. func ScanDirectory(dir string) (*Registry, error) {
func ScanDirectory(m io.Medium, dir string) (*Registry, error) { entries, err := io.Local.List(dir)
entries, err := m.List(dir)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read directory: %w", err) return nil, fmt.Errorf("failed to read directory: %w", err)
} }
@ -151,7 +142,6 @@ func ScanDirectory(m io.Medium, dir string) (*Registry, error) {
Version: 1, Version: 1,
BasePath: dir, BasePath: dir,
Repos: make(map[string]*Repo), Repos: make(map[string]*Repo),
medium: m,
} }
// Try to detect org from git remote // Try to detect org from git remote
@ -163,22 +153,21 @@ func ScanDirectory(m io.Medium, dir string) (*Registry, error) {
repoPath := filepath.Join(dir, entry.Name()) repoPath := filepath.Join(dir, entry.Name())
gitPath := filepath.Join(repoPath, ".git") gitPath := filepath.Join(repoPath, ".git")
if !m.IsDir(gitPath) { if !io.Local.IsDir(gitPath) {
continue // Not a git repo continue // Not a git repo
} }
repo := &Repo{ repo := &Repo{
Name: entry.Name(), Name: entry.Name(),
Path: repoPath, Path: repoPath,
Type: "module", // Default type Type: "module", // Default type
registry: reg,
} }
reg.Repos[entry.Name()] = repo reg.Repos[entry.Name()] = repo
// Try to detect org from first repo's remote // Try to detect org from first repo's remote
if reg.Org == "" { if reg.Org == "" {
reg.Org = detectOrg(m, repoPath) reg.Org = detectOrg(repoPath)
} }
} }
@ -186,13 +175,15 @@ func ScanDirectory(m io.Medium, dir string) (*Registry, error) {
} }
// detectOrg tries to extract the GitHub org from a repo's origin remote. // detectOrg tries to extract the GitHub org from a repo's origin remote.
func detectOrg(m io.Medium, repoPath string) string { func detectOrg(repoPath string) string {
// Try to read git remote // Try to read git remote
configPath := filepath.Join(repoPath, ".git", "config") configPath := filepath.Join(repoPath, ".git", "config")
content, err := m.Read(configPath) content, err := io.Local.Read(configPath)
if err != nil { if err != nil {
return "" return ""
} }
// Simple parse for github.com URLs
// Look for patterns like github.com:org/repo or github.com/org/repo // Look for patterns like github.com:org/repo or github.com/org/repo
for _, line := range strings.Split(content, "\n") { for _, line := range strings.Split(content, "\n") {
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
@ -301,20 +292,13 @@ func (r *Registry) TopologicalOrder() ([]*Repo, error) {
// Exists checks if the repo directory exists on disk. // Exists checks if the repo directory exists on disk.
func (repo *Repo) Exists() bool { func (repo *Repo) Exists() bool {
return repo.getMedium().IsDir(repo.Path) return io.Local.IsDir(repo.Path)
} }
// IsGitRepo checks if the repo directory contains a .git folder. // IsGitRepo checks if the repo directory contains a .git folder.
func (repo *Repo) IsGitRepo() bool { func (repo *Repo) IsGitRepo() bool {
gitPath := filepath.Join(repo.Path, ".git") gitPath := filepath.Join(repo.Path, ".git")
return repo.getMedium().IsDir(gitPath) return io.Local.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. // expandPath expands ~ to home directory.