package settings import ( "bytes" "io/fs" "os" "github.com/pelletier/go-toml" "git.destrealm.net/go/cwrap" ) type Settings struct { API API DailyScripture DailyScripture `toml:"daily_scripture"` System System Translations Translations Chat Chat } type API struct { BaseURL string `toml:"base_url"` Schedule string Rotation string User string } type Chat struct { Channels []int64 `toml:"channels"` } type DailyScripture struct { UserSubmitted bool `toml:"user_submitted"` Schedule string Rotation string User string } type System struct { TimeZone string `toml:"timezone"` } type Service struct { Method string Webhook *Webhook } type Webhook struct { URI string Port int Host string } type Translations struct { BaseDir string `toml:"basedir"` Active []string `toml:"active"` } func (c *Settings) Marshal() ([]byte, error) { buf := &bytes.Buffer{} enc := toml.NewEncoder(cwrap.New(buf)) enc.ArraysWithOneElementPerLine(true) enc.Indentation(" ") enc.Order(toml.OrderPreserve) enc.PromoteAnonymous(false) if err := enc.Encode(c); err != nil { return nil, err } return buf.Bytes(), nil } func LoadSettings(name string) (*Settings, error) { conf := &Settings{} buf, err := os.ReadFile(name) if err != nil { if _, ok := err.(*fs.PathError); ok { return nil, ErrSettingsNotFound.Do(err) } return nil, ErrReadingSettings.Do(err) } err = toml.Unmarshal(buf, conf) if err != nil { return nil, ErrReadingSettings.Do(err) } return conf, nil } func SaveSettings(name string, settings *Settings) error { buf, err := settings.Marshal() if err != nil { return err } return os.WriteFile(name, buf, 0o644) }