From cb7bd792f0815be23fa1bbe12fbfe7a63f71582e Mon Sep 17 00:00:00 2001 From: Snider Date: Mon, 2 Feb 2026 00:33:00 +0000 Subject: [PATCH] chore(io): Migrate internal/cmd/setup/* to Medium abstraction --- internal/cmd/setup/cmd_bootstrap.go | 9 +++++---- internal/cmd/setup/cmd_ci.go | 5 +++-- internal/cmd/setup/cmd_registry.go | 8 +++++--- internal/cmd/setup/cmd_repo.go | 14 +++++++------- internal/cmd/setup/github_config.go | 9 +++++---- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/internal/cmd/setup/cmd_bootstrap.go b/internal/cmd/setup/cmd_bootstrap.go index 2e902b4c..a7d354ec 100644 --- a/internal/cmd/setup/cmd_bootstrap.go +++ b/internal/cmd/setup/cmd_bootstrap.go @@ -15,6 +15,7 @@ import ( "github.com/host-uk/core/internal/cmd/workspace" "github.com/host-uk/core/pkg/i18n" + coreio "github.com/host-uk/core/pkg/io" "github.com/host-uk/core/pkg/repos" ) @@ -96,7 +97,7 @@ func runBootstrap(ctx context.Context, only string, dryRun, all bool, projectNam fmt.Printf("%s %s: %s\n", dimStyle.Render(">>"), i18n.T("cmd.setup.creating_project_dir"), projectName) if !dryRun { - if err := os.MkdirAll(targetDir, 0755); err != nil { + if err := coreio.Local.EnsureDir(targetDir); err != nil { return fmt.Errorf("failed to create directory: %w", err) } } @@ -104,7 +105,7 @@ func runBootstrap(ctx context.Context, only string, dryRun, all bool, projectNam // Clone core-devops first devopsPath := filepath.Join(targetDir, devopsRepo) - if _, err := os.Stat(filepath.Join(devopsPath, ".git")); os.IsNotExist(err) { + if _, err := coreio.Local.List(filepath.Join(devopsPath, ".git")); err != nil { fmt.Printf("%s %s %s...\n", dimStyle.Render(">>"), i18n.T("common.status.cloning"), devopsRepo) if !dryRun { @@ -148,13 +149,13 @@ func runBootstrap(ctx context.Context, only string, dryRun, all bool, projectNam // isGitRepoRoot returns true if the directory is a git repository root. func isGitRepoRoot(path string) bool { - _, err := os.Stat(filepath.Join(path, ".git")) + _, err := coreio.Local.List(filepath.Join(path, ".git")) return err == nil } // isDirEmpty returns true if the directory is empty or contains only hidden files. func isDirEmpty(path string) (bool, error) { - entries, err := os.ReadDir(path) + entries, err := coreio.Local.List(path) if err != nil { return false, err } diff --git a/internal/cmd/setup/cmd_ci.go b/internal/cmd/setup/cmd_ci.go index cad86332..11ca0eac 100644 --- a/internal/cmd/setup/cmd_ci.go +++ b/internal/cmd/setup/cmd_ci.go @@ -7,6 +7,7 @@ import ( "runtime" "github.com/host-uk/core/pkg/cli" + coreio "github.com/host-uk/core/pkg/io" "github.com/spf13/cobra" "gopkg.in/yaml.v3" ) @@ -51,9 +52,9 @@ func LoadCIConfig() *CIConfig { for { configPath := filepath.Join(dir, ".core", "ci.yaml") - data, err := os.ReadFile(configPath) + data, err := coreio.Local.Read(configPath) if err == nil { - if err := yaml.Unmarshal(data, cfg); err == nil { + if err := yaml.Unmarshal([]byte(data), cfg); err == nil { return cfg } } diff --git a/internal/cmd/setup/cmd_registry.go b/internal/cmd/setup/cmd_registry.go index e68fc2b9..896a4e63 100644 --- a/internal/cmd/setup/cmd_registry.go +++ b/internal/cmd/setup/cmd_registry.go @@ -16,6 +16,7 @@ import ( "github.com/host-uk/core/internal/cmd/workspace" "github.com/host-uk/core/pkg/cli" "github.com/host-uk/core/pkg/i18n" + coreio "github.com/host-uk/core/pkg/io" "github.com/host-uk/core/pkg/repos" ) @@ -80,7 +81,7 @@ func runRegistrySetupWithReg(ctx context.Context, reg *repos.Registry, registryP // Ensure base path exists if !dryRun { - if err := os.MkdirAll(basePath, 0755); err != nil { + if err := coreio.Local.EnsureDir(basePath); err != nil { return fmt.Errorf("failed to create packages directory: %w", err) } } @@ -116,7 +117,8 @@ func runRegistrySetupWithReg(ctx context.Context, reg *repos.Registry, registryP // Check if already exists repoPath := filepath.Join(basePath, repo.Name) - if _, err := os.Stat(filepath.Join(repoPath, ".git")); err == nil { + // Check .git dir existence via List + if _, err := coreio.Local.List(filepath.Join(repoPath, ".git")); err == nil { exists++ continue } @@ -145,7 +147,7 @@ func runRegistrySetupWithReg(ctx context.Context, reg *repos.Registry, registryP // Check if already exists repoPath := filepath.Join(basePath, repo.Name) - if _, err := os.Stat(filepath.Join(repoPath, ".git")); err == nil { + if _, err := coreio.Local.List(filepath.Join(repoPath, ".git")); err == nil { exists++ continue } diff --git a/internal/cmd/setup/cmd_repo.go b/internal/cmd/setup/cmd_repo.go index 330313a0..c8159697 100644 --- a/internal/cmd/setup/cmd_repo.go +++ b/internal/cmd/setup/cmd_repo.go @@ -8,12 +8,12 @@ package setup import ( "fmt" - "os" "os/exec" "path/filepath" "strings" "github.com/host-uk/core/pkg/i18n" + coreio "github.com/host-uk/core/pkg/io" ) // runRepoSetup sets up the current repository with .core/ configuration. @@ -27,7 +27,7 @@ func runRepoSetup(repoPath string, dryRun bool) error { // Create .core directory coreDir := filepath.Join(repoPath, ".core") if !dryRun { - if err := os.MkdirAll(coreDir, 0755); err != nil { + if err := coreio.Local.EnsureDir(coreDir); err != nil { return fmt.Errorf("failed to create .core directory: %w", err) } } @@ -54,7 +54,7 @@ func runRepoSetup(repoPath string, dryRun bool) error { for filename, content := range configs { configPath := filepath.Join(coreDir, filename) - if err := os.WriteFile(configPath, []byte(content), 0644); err != nil { + if err := coreio.Local.Write(configPath, content); err != nil { return fmt.Errorf("failed to write %s: %w", filename, err) } fmt.Printf("%s %s %s\n", successStyle.Render(">>"), i18n.T("cmd.setup.repo.created"), configPath) @@ -66,16 +66,16 @@ func runRepoSetup(repoPath string, dryRun bool) error { // detectProjectType identifies the project type from files present. func detectProjectType(path string) string { // Check in priority order - if _, err := os.Stat(filepath.Join(path, "wails.json")); err == nil { + if coreio.Local.IsFile(filepath.Join(path, "wails.json")) { return "wails" } - if _, err := os.Stat(filepath.Join(path, "go.mod")); err == nil { + if coreio.Local.IsFile(filepath.Join(path, "go.mod")) { return "go" } - if _, err := os.Stat(filepath.Join(path, "composer.json")); err == nil { + if coreio.Local.IsFile(filepath.Join(path, "composer.json")) { return "php" } - if _, err := os.Stat(filepath.Join(path, "package.json")); err == nil { + if coreio.Local.IsFile(filepath.Join(path, "package.json")) { return "node" } return "unknown" diff --git a/internal/cmd/setup/github_config.go b/internal/cmd/setup/github_config.go index 3c673450..7c12795a 100644 --- a/internal/cmd/setup/github_config.go +++ b/internal/cmd/setup/github_config.go @@ -12,6 +12,7 @@ import ( "regexp" "strings" + coreio "github.com/host-uk/core/pkg/io" "gopkg.in/yaml.v3" ) @@ -64,13 +65,13 @@ type SecurityConfig struct { // LoadGitHubConfig reads and parses a GitHub configuration file. func LoadGitHubConfig(path string) (*GitHubConfig, error) { - data, err := os.ReadFile(path) + data, err := coreio.Local.Read(path) if err != nil { return nil, fmt.Errorf("failed to read config file: %w", err) } // Expand environment variables before parsing - expanded := expandEnvVars(string(data)) + expanded := expandEnvVars(data) var config GitHubConfig if err := yaml.Unmarshal([]byte(expanded), &config); err != nil { @@ -127,7 +128,7 @@ func expandEnvVars(input string) string { // 3. github.yaml (relative to registry) func FindGitHubConfig(registryDir, specifiedPath string) (string, error) { if specifiedPath != "" { - if _, err := os.Stat(specifiedPath); err == nil { + if coreio.Local.IsFile(specifiedPath) { return specifiedPath, nil } return "", fmt.Errorf("config file not found: %s", specifiedPath) @@ -140,7 +141,7 @@ func FindGitHubConfig(registryDir, specifiedPath string) (string, error) { } for _, path := range candidates { - if _, err := os.Stat(path); err == nil { + if coreio.Local.IsFile(path) { return path, nil } }