2026-09-09 16:19:41 -06:00
|
|
|
package settings
|
|
|
|
|
|
|
|
|
|
import (
|
2026-09-10 22:31:23 -06:00
|
|
|
"bytes"
|
2026-09-09 22:14:55 -06:00
|
|
|
"io/fs"
|
|
|
|
|
"os"
|
|
|
|
|
|
2026-09-09 16:19:41 -06:00
|
|
|
"github.com/pelletier/go-toml"
|
2026-09-10 22:31:23 -06:00
|
|
|
|
|
|
|
|
"git.destrealm.net/go/cwrap"
|
2026-09-09 16:19:41 -06:00
|
|
|
)
|
|
|
|
|
|
2026-09-09 22:14:55 -06:00
|
|
|
type Settings struct {
|
|
|
|
|
API API
|
2026-09-09 16:19:41 -06:00
|
|
|
DailyScripture DailyScripture `toml:"daily_scripture"`
|
2026-09-09 22:14:55 -06:00
|
|
|
System System
|
|
|
|
|
Translations Translations
|
2026-09-10 22:31:23 -06:00
|
|
|
Chat Chat
|
2026-09-09 16:19:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type API struct {
|
2026-09-09 22:14:55 -06:00
|
|
|
BaseURL string `toml:"base_url"`
|
2026-09-09 16:19:41 -06:00
|
|
|
Schedule string
|
|
|
|
|
Rotation string
|
2026-09-09 22:14:55 -06:00
|
|
|
User string
|
2026-09-09 16:19:41 -06:00
|
|
|
}
|
|
|
|
|
|
2026-09-10 22:31:23 -06:00
|
|
|
type Chat struct {
|
|
|
|
|
Channels []string `toml:"channels"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-09 16:19:41 -06:00
|
|
|
type DailyScripture struct {
|
|
|
|
|
UserSubmitted bool `toml:"user_submitted"`
|
2026-09-10 22:31:23 -06:00
|
|
|
Schedule string
|
|
|
|
|
Rotation string
|
|
|
|
|
User string
|
2026-09-09 16:19:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type System struct {
|
|
|
|
|
TimeZone string `toml:"timezone"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Translations struct {
|
2026-09-09 22:14:55 -06:00
|
|
|
BaseDir string `toml:"basedir"`
|
|
|
|
|
Active []string `toml:"active"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 22:31:23 -06:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-09 22:14:55 -06:00
|
|
|
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
|
|
|
|
|
}
|
2026-09-10 22:31:23 -06:00
|
|
|
|
|
|
|
|
func SaveSettings(name string, settings *Settings) error {
|
|
|
|
|
buf, err := settings.Marshal()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return os.WriteFile(name, buf, 0o644)
|
|
|
|
|
}
|