package main import ( "encoding/json" "fmt" "io/ioutil" "log/slog" "os" "strings" "time" ) type Datastore struct { Data map[string]string `json:"data"` LastUpdate time.Time `json:"updated"` } func FromFile(cachePath string) *Datastore { bytes, err := ioutil.ReadFile(cachePath) if err != nil { return nil } var store Datastore json.Unmarshal(bytes, &store) return &store } func (d Datastore) List(namespace string) map[string]string { slog.Debug(fmt.Sprintf("Listing entries with namespace: %s", namespace)) withinNamespace := map[string]string{} for key, value := range d.Data { if strings.HasPrefix(key, namespace) { withinNamespace[key] = value } } return withinNamespace } func (d Datastore) Get(key string) string { slog.Debug(fmt.Sprintf("Getting entry with key: %s", key)) return d.Data[key] } func (d *Datastore) Set(key string, value string) { slog.Debug(fmt.Sprintf("Setting entry for key %s", key)) d.Data[key] = value os.WriteFile("datastore.json", []byte(d.Serialize()), 0755) } func (d Datastore) Serialize() string { slog.Debug("Serialized state") b, _ := json.Marshal(d) return string(b) }