From cfb7dee74180bdb99accb00c14284b8875b95ad7 Mon Sep 17 00:00:00 2001 From: Snider Date: Mon, 2 Feb 2026 04:32:20 +0000 Subject: [PATCH] feat(cache): migrate filesystem operations to io.Local abstraction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace direct os package calls with io.Local methods: - os.MkdirAll → io.Local.EnsureDir - os.ReadFile → io.Local.Read - os.WriteFile → io.Local.Write - os.Remove → io.Local.Delete - os.RemoveAll → io.Local.DeleteAll This is part of the io.Medium abstraction migration (#101). Closes #104 Co-Authored-By: Claude Opus 4.5 --- pkg/cache/cache.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 6081fc3..91d8c29 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" "time" + + "github.com/host-uk/core/pkg/io" ) // DefaultTTL is the default cache expiry time. @@ -41,7 +43,7 @@ func New(baseDir string, ttl time.Duration) (*Cache, error) { } // Ensure cache directory exists - if err := os.MkdirAll(baseDir, 0755); err != nil { + if err := io.Local.EnsureDir(baseDir); err != nil { return nil, err } @@ -60,7 +62,7 @@ func (c *Cache) Path(key string) string { func (c *Cache) Get(key string, dest interface{}) (bool, error) { path := c.Path(key) - data, err := os.ReadFile(path) + dataStr, err := io.Local.Read(path) if err != nil { if os.IsNotExist(err) { return false, nil @@ -69,7 +71,7 @@ func (c *Cache) Get(key string, dest interface{}) (bool, error) { } var entry Entry - if err := json.Unmarshal(data, &entry); err != nil { + if err := json.Unmarshal([]byte(dataStr), &entry); err != nil { // Invalid cache file, treat as miss return false, nil } @@ -92,7 +94,7 @@ func (c *Cache) Set(key string, data interface{}) error { path := c.Path(key) // Ensure parent directory exists - if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { + if err := io.Local.EnsureDir(filepath.Dir(path)); err != nil { return err } @@ -113,13 +115,13 @@ func (c *Cache) Set(key string, data interface{}) error { return err } - return os.WriteFile(path, entryBytes, 0644) + return io.Local.Write(path, string(entryBytes)) } // Delete removes an item from the cache. func (c *Cache) Delete(key string) error { path := c.Path(key) - err := os.Remove(path) + err := io.Local.Delete(path) if os.IsNotExist(err) { return nil } @@ -128,20 +130,20 @@ func (c *Cache) Delete(key string) error { // Clear removes all cached items. func (c *Cache) Clear() error { - return os.RemoveAll(c.baseDir) + return io.Local.DeleteAll(c.baseDir) } // Age returns how old a cached item is, or -1 if not cached. func (c *Cache) Age(key string) time.Duration { path := c.Path(key) - data, err := os.ReadFile(path) + dataStr, err := io.Local.Read(path) if err != nil { return -1 } var entry Entry - if err := json.Unmarshal(data, &entry); err != nil { + if err := json.Unmarshal([]byte(dataStr), &entry); err != nil { return -1 }