mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2026-05-14 10:52:08 +00:00
Instead of storing cached values in the config yaml, they are now stored under these OS specific locations: * Linux: `~/.cache/k8sgpt` * MacOS: `~/Library/Caches` * Windows: `%LocalAppData%\cache` Additionally a `Cache` package and interface has been introduced. Currently there are two implementations: * Noop - Doesn't do anything * FileBased - Stores data in files under the locations listed above fixes #323 Signed-off-by: Patrick Pichler <git@patrickpichler.dev>
59 lines
1.0 KiB
Go
59 lines
1.0 KiB
Go
package cache
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/adrg/xdg"
|
|
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
|
|
)
|
|
|
|
var _ (ICache) = (*FileBasedCache)(nil)
|
|
|
|
type FileBasedCache struct{}
|
|
|
|
func (*FileBasedCache) Exists(key string) bool {
|
|
path, err := xdg.CacheFile(filepath.Join("k8sgpt", key))
|
|
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "warning: error while testing if cache key exists:", err)
|
|
return false
|
|
}
|
|
|
|
exists, err := util.FileExists(path)
|
|
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "warning: error while testing if cache key exists:", err)
|
|
return false
|
|
}
|
|
|
|
return exists
|
|
}
|
|
|
|
func (*FileBasedCache) Load(key string) (string, error) {
|
|
path, err := xdg.CacheFile(filepath.Join("k8sgpt", key))
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(data), nil
|
|
}
|
|
|
|
func (*FileBasedCache) Store(key string, data string) error {
|
|
path, err := xdg.CacheFile(filepath.Join("k8sgpt", key))
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return os.WriteFile(path, []byte(data), 0600)
|
|
}
|