mukan-ignite/ignite/internal/tools/gen-mig-diffs/pkg/cache/cache.go
Mukan Erkin Törük c32551b6f7
Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
refactor: replace all github.com upstream refs with git.cw.tr/mukan-network
2026-05-11 03:36:24 +03:00

77 lines
1.7 KiB
Go

package cache
import (
"os"
"path/filepath"
"sync"
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/errors"
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/xos"
)
// Cache represents a cache for executed scaffold command.
type Cache struct {
cachePath string
cachesPath map[string]string
mu sync.RWMutex
}
// New initializes a new Cache instance.
func New(path string) (*Cache, error) {
return &Cache{
cachePath: path,
cachesPath: make(map[string]string),
}, os.MkdirAll(path, os.ModePerm)
}
// Save creates a new cache.
func (c *Cache) Save(name, path string) error {
c.mu.Lock()
defer c.mu.Unlock()
dstPath := filepath.Join(c.cachePath, name)
if err := xos.CopyFolder(path, dstPath); err != nil {
return err
}
c.cachesPath[name] = dstPath
return nil
}
// Has return if the cache exist.
func (c *Cache) Has(name string) bool {
c.mu.RLock()
defer c.mu.RUnlock()
cachePath, ok := c.cachesPath[name]
if !ok {
return false
}
if _, err := os.Stat(cachePath); os.IsNotExist(err) {
return false
}
return true
}
// Get return the cache path and copy all files to the destination path.
func (c *Cache) Get(name, dstPath string) error {
c.mu.RLock()
defer c.mu.RUnlock()
cachePath, ok := c.cachesPath[name]
if !ok {
return errors.Errorf("command %s not exist in the cache list", name)
}
if _, err := os.Stat(cachePath); os.IsNotExist(err) {
return errors.Wrapf(err, "cache %s not exist in the path", name)
}
dstPath, err := filepath.Abs(dstPath)
if err != nil {
return err
}
if err := xos.CopyFolder(cachePath, dstPath); err != nil {
return errors.Wrapf(err, "error to copy cache from %s to %s", cachePath, dstPath)
}
return nil
}