From bd665e0892cb67f2fcb1b6736de2804e5e9ce2a1 Mon Sep 17 00:00:00 2001 From: Snider Date: Mon, 2 Feb 2026 04:35:09 +0000 Subject: [PATCH] feat(container): migrate filesystem operations to io.Local abstraction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migrate state.go: - os.ReadFile → io.Local.Read - os.MkdirAll → io.Local.EnsureDir - os.WriteFile → io.Local.Write Migrate templates.go: - os.Stat → io.Local.IsFile/IsDir - os.ReadFile → io.Local.Read - os.ReadDir → io.Local.List Part of #101 (io.Medium migration tracking issue). Closes #108 Co-Authored-By: Claude Opus 4.5 --- pkg/container/state.go | 12 +++++++----- pkg/container/templates.go | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/pkg/container/state.go b/pkg/container/state.go index b8d98b97..5b4e1e7f 100644 --- a/pkg/container/state.go +++ b/pkg/container/state.go @@ -5,6 +5,8 @@ import ( "os" "path/filepath" "sync" + + "github.com/host-uk/core/pkg/io" ) // State manages persistent container state. @@ -56,7 +58,7 @@ func NewState(filePath string) *State { func LoadState(filePath string) (*State, error) { state := NewState(filePath) - data, err := os.ReadFile(filePath) + dataStr, err := io.Local.Read(filePath) if err != nil { if os.IsNotExist(err) { return state, nil @@ -64,7 +66,7 @@ func LoadState(filePath string) (*State, error) { return nil, err } - if err := json.Unmarshal(data, state); err != nil { + if err := json.Unmarshal([]byte(dataStr), state); err != nil { return nil, err } @@ -78,7 +80,7 @@ func (s *State) SaveState() error { // Ensure the directory exists dir := filepath.Dir(s.filePath) - if err := os.MkdirAll(dir, 0755); err != nil { + if err := io.Local.EnsureDir(dir); err != nil { return err } @@ -87,7 +89,7 @@ func (s *State) SaveState() error { return err } - return os.WriteFile(s.filePath, data, 0644) + return io.Local.Write(s.filePath, string(data)) } // Add adds a container to the state and persists it. @@ -166,5 +168,5 @@ func EnsureLogsDir() error { if err != nil { return err } - return os.MkdirAll(logsDir, 0755) + return io.Local.EnsureDir(logsDir) } diff --git a/pkg/container/templates.go b/pkg/container/templates.go index b0068a00..80ec3005 100644 --- a/pkg/container/templates.go +++ b/pkg/container/templates.go @@ -7,6 +7,8 @@ import ( "path/filepath" "regexp" "strings" + + "github.com/host-uk/core/pkg/io" ) //go:embed templates/*.yml @@ -71,12 +73,12 @@ func GetTemplate(name string) (string, error) { userTemplatesDir := getUserTemplatesDir() if userTemplatesDir != "" { templatePath := filepath.Join(userTemplatesDir, name+".yml") - if _, err := os.Stat(templatePath); err == nil { - content, err := os.ReadFile(templatePath) + if io.Local.IsFile(templatePath) { + content, err := io.Local.Read(templatePath) if err != nil { return "", fmt.Errorf("failed to read user template %s: %w", name, err) } - return string(content), nil + return content, nil } } @@ -194,7 +196,7 @@ func getUserTemplatesDir() string { cwd, err := os.Getwd() if err == nil { wsDir := filepath.Join(cwd, ".core", "linuxkit") - if info, err := os.Stat(wsDir); err == nil && info.IsDir() { + if io.Local.IsDir(wsDir) { return wsDir } } @@ -206,7 +208,7 @@ func getUserTemplatesDir() string { } homeDir := filepath.Join(home, ".core", "linuxkit") - if info, err := os.Stat(homeDir); err == nil && info.IsDir() { + if io.Local.IsDir(homeDir) { return homeDir } @@ -217,7 +219,7 @@ func getUserTemplatesDir() string { func scanUserTemplates(dir string) []Template { var templates []Template - entries, err := os.ReadDir(dir) + entries, err := io.Local.List(dir) if err != nil { return templates } @@ -266,12 +268,12 @@ func scanUserTemplates(dir string) []Template { // extractTemplateDescription reads the first comment block from a YAML file // to use as a description. func extractTemplateDescription(path string) string { - content, err := os.ReadFile(path) + content, err := io.Local.Read(path) if err != nil { return "" } - lines := strings.Split(string(content), "\n") + lines := strings.Split(content, "\n") var descLines []string for _, line := range lines {