diff options
| author | hachem <im@hachem.wtf> | 2026-08-29 10:44:22 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-08-29 10:44:22 +0200 |
| commit | af4a6f4c5e9a3e3a501deac71d92bbbcd12ef81f (patch) | |
| tree | 4882c0dd5e7e252e6216e00cc5cba0e6c2bfd533 /internal/config/cache.go | |
| parent | 25bdd381d6dcf8581b1391c1c1fa22c108913ccb (diff) | |
add: reimplement discord bot
Diffstat (limited to 'internal/config/cache.go')
| -rw-r--r-- | internal/config/cache.go | 63 |
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) +} |
