Files
LazyUpdateManager/internal/state/state.go

47 lines
929 B
Go

package state
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"time"
)
type Store struct {
LastReminder time.Time `json:"last_reminder"`
LastUpdateCount int `json:"last_update_count"`
LastCheck time.Time `json:"last_check"`
LastSuccess time.Time `json:"last_success"`
LastSummary string `json:"last_summary"`
}
func Load(path string) (Store, error) {
data, err := os.ReadFile(path)
if errors.Is(err, os.ErrNotExist) {
return Store{}, nil
}
if err != nil {
return Store{}, err
}
var store Store
if err := json.Unmarshal(data, &store); err != nil {
return Store{}, err
}
return store, nil
}
func Save(path string, store Store) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
data, err := json.MarshalIndent(store, "", " ")
if err != nil {
return err
}
data = append(data, '\n')
return os.WriteFile(path, data, 0o644)
}