k8sgpt/pkg/cache/file_based.go
Matthis 12146bf356
feat: rework cache package - add gcs cache - add cache purge command (#750)
* feat: rework cache pkg

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: Completion of cache pkg rework. Added cache purge command.

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* doc: add purgin command note

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* fix: disable cache if noCache is set

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: improve GetCacheConfiguration lisibility & transform add method to addOrUpdate

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: transform server mode to work with new cache configuration

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* fix: use 'switch' instead 'if' to evaluate Cache from grpc

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: add mutually exclusive flags for command options

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* doc: update readme.md

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: return err on bucket creation failed

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: update dependencies

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

---------

Signed-off-by: Matthis Holleville <matthish29@gmail.com>
Signed-off-by: Matthis <matthish29@gmail.com>
2023-11-18 22:08:38 +01:00

117 lines
2.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 {
noCache bool
}
func (f *FileBasedCache) Configure(cacheInfo CacheProvider) error {
return nil
}
func (f *FileBasedCache) IsCacheDisabled() bool {
return f.noCache
}
func (*FileBasedCache) List() ([]CacheObjectDetails, error) {
path, err := xdg.CacheFile("k8sgpt")
if err != nil {
return nil, err
}
files, err := os.ReadDir(path)
if err != nil {
return nil, err
}
var result []CacheObjectDetails
for _, file := range files {
info, err := file.Info()
if err != nil {
return nil, err
}
result = append(result, CacheObjectDetails{
Name: file.Name(),
UpdatedAt: info.ModTime(),
})
}
return result, nil
}
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) Remove(key string) error {
path, err := xdg.CacheFile(filepath.Join("k8sgpt", key))
if err != nil {
return err
}
if err := os.Remove(path); err != nil {
return err
}
return 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)
}
func (s *FileBasedCache) GetName() string {
return "file"
}
func (s *FileBasedCache) DisableCache() {
s.noCache = true
}