1
0
Fork 0
mirror of https://github.com/Luzifer/automail.git synced 2024-12-20 21:11:20 +00:00
automail/store.go

44 lines
907 B
Go
Raw Normal View History

2020-06-27 13:25:16 +00:00
package main
import (
"os"
"path"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type storage struct {
LastUID uint32
}
func loadStorage() (*storage, error) {
var out = &storage{}
if _, err := os.Stat(cfg.StorageFile); os.IsNotExist(err) {
return out, nil
}
f, err := os.Open(cfg.StorageFile)
if err != nil {
return nil, errors.Wrap(err, "Failed to open storage file")
}
defer f.Close()
return out, errors.Wrap(yaml.NewDecoder(f).Decode(out), "Unable to decode storage file")
}
func (s storage) saveStorage() error {
if err := os.MkdirAll(path.Dir(cfg.StorageFile), 0700); err != nil {
return errors.Wrap(err, "Unable to ensure directory for storage file")
}
f, err := os.Create(cfg.StorageFile)
if err != nil {
return errors.Wrap(err, "Unable to create storage file")
}
defer f.Close()
return errors.Wrap(yaml.NewEncoder(f).Encode(s), "Unable to encode storage file")
}