hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/internal/config/cache.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/config/cache.go')
-rw-r--r--internal/config/cache.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/internal/config/cache.go b/internal/config/cache.go
new file mode 100644
index 0000000..825af7f
--- /dev/null
+++ b/internal/config/cache.go
@@ -0,0 +1,63 @@
+package config
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+)
+
+type API struct {
+ Token string `json:"token"`
+}
+
+func LoadAPI() (*API, error) {
+ data, err := os.ReadFile(APIPath())
+ if err != nil {
+ return nil, err
+ }
+
+ var a API
+ if err := json.Unmarshal(data, &a); err != nil {
+ return nil, err
+ }
+ return &a, nil
+}
+
+type ModState struct {
+ Compatible string `json:"compatible"`
+ Latest string `json:"latest"`
+}
+
+type Cache map[string]ModState
+
+func CachePath() string {
+ return filepath.Join("cache", "mod_versions.json")
+}
+
+func LoadCache() (Cache, error) {
+ data, err := os.ReadFile(CachePath())
+ if err != nil {
+ if os.IsNotExist(err) {
+ return Cache{}, nil
+ }
+ return nil, err
+ }
+
+ cache := Cache{}
+ if err := json.Unmarshal(data, &cache); err != nil {
+ return nil, err
+ }
+ return cache, nil
+}
+
+func SaveCache(cache Cache) error {
+ if err := os.MkdirAll(filepath.Dir(CachePath()), 0o755); err != nil {
+ return err
+ }
+
+ data, err := json.MarshalIndent(cache, "", " ")
+ if err != nil {
+ return err
+ }
+ return os.WriteFile(CachePath(), data, 0o644)
+}