chore(io): Migrate internal/cmd/setup/* to Medium abstraction

This commit is contained in:
Snider 2026-02-02 00:33:00 +00:00
parent 96e8d0dad5
commit 197abcd860
5 changed files with 25 additions and 20 deletions

View file

@ -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
}

View file

@ -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
}
}

View file

@ -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
}

View file

@ -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"

View file

@ -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
}
}