2026-09-09 16:19:41 -06:00
|
|
|
package settings
|
|
|
|
|
|
|
|
|
|
import (
|
2026-09-10 22:31:23 -06:00
|
|
|
"bytes"
|
2026-10-02 18:00:29 -06:00
|
|
|
"fmt"
|
2026-09-09 22:14:55 -06:00
|
|
|
"io/fs"
|
2026-10-02 18:00:29 -06:00
|
|
|
"net"
|
2026-09-09 22:14:55 -06:00
|
|
|
"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 {
|
2026-09-12 15:41:20 -06:00
|
|
|
API *API `toml:"api"`
|
|
|
|
|
DailyScripture *DailyScripture `toml:"daily_scripture"`
|
|
|
|
|
Translations *Translations `toml:"translations"`
|
|
|
|
|
System *System `toml:"system"`
|
|
|
|
|
Service *Service `toml:"service"`
|
|
|
|
|
Chat *Chat `toml:"chat"`
|
2026-09-09 16:19:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type API struct {
|
2026-09-12 15:41:20 -06:00
|
|
|
BaseURL string `toml:"base_url"`
|
2026-09-09 16:19:41 -06:00
|
|
|
}
|
|
|
|
|
|
2026-09-10 22:31:23 -06:00
|
|
|
type Chat struct {
|
2026-09-11 15:20:32 -06:00
|
|
|
Channels []int64 `toml:"channels"`
|
2026-09-10 22:31:23 -06:00
|
|
|
}
|
|
|
|
|
|
2026-09-09 16:19:41 -06:00
|
|
|
type DailyScripture struct {
|
2026-09-12 15:41:20 -06:00
|
|
|
UserSubmitted bool `toml:"user_submitted"`
|
|
|
|
|
Schedule string `toml:"schedule"`
|
2026-09-09 16:19:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type System struct {
|
|
|
|
|
TimeZone string `toml:"timezone"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 19:57:32 -06:00
|
|
|
type Service struct {
|
2026-09-12 15:41:20 -06:00
|
|
|
Method string `toml:"method"`
|
|
|
|
|
Webhook *Webhook `toml:"webhook"`
|
2026-09-11 19:57:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Webhook struct {
|
2026-09-12 15:41:20 -06:00
|
|
|
URI string `toml:"uri"`
|
|
|
|
|
Port int `toml:"port"`
|
|
|
|
|
Host string `toml:"host"`
|
2026-09-11 19:57:32 -06:00
|
|
|
}
|
|
|
|
|
|
2026-10-02 18:00:29 -06:00
|
|
|
func (w *Webhook) ListenAddr() string {
|
|
|
|
|
return net.JoinHostPort(w.Host, fmt.Sprintf("%d", w.Port))
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-09 16:19:41 -06:00
|
|
|
type Translations struct {
|
2026-09-09 22:14:55 -06:00
|
|
|
BaseDir string `toml:"basedir"`
|
2026-09-25 23:16:55 -06:00
|
|
|
Current string `toml:"current"`
|
2026-09-09 22:14:55 -06:00
|
|
|
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)
|
|
|
|
|
}
|