forked from k8sgpt-ai/k8sgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use OS conform path for storing cached results
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 k8sgpt-ai#323
- Loading branch information
Patrick Pichler
committed
Apr 24, 2023
1 parent
e7f74db
commit 4c0383f
Showing
7 changed files
with
120 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cache | ||
|
||
type Cache interface { | ||
Store(key string, data string) error | ||
Load(key string) (string, error) | ||
Exists(key string) bool | ||
} | ||
|
||
func New(noCache bool) Cache { | ||
if noCache { | ||
return &NoopCache{} | ||
} | ||
|
||
return &FileBasedCache{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/adrg/xdg" | ||
"github.com/k8sgpt-ai/k8sgpt/pkg/util" | ||
) | ||
|
||
var _ (Cache) = (*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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cache | ||
|
||
var _ (Cache) = (*NoopCache)(nil) | ||
|
||
type NoopCache struct{} | ||
|
||
func (c *NoopCache) Store(key string, data string) error { | ||
return nil | ||
} | ||
|
||
func (c *NoopCache) Load(key string) (string, error) { | ||
return "", nil | ||
} | ||
|
||
func (c *NoopCache) Exists(key string) bool { | ||
return false | ||
} |