justusbot/internal/settings/settings.go

185 lines
3.7 KiB
Go
Raw Normal View History

package settings
import (
2026-09-10 22:31:23 -06:00
"bytes"
"fmt"
2026-10-05 15:43:05 -06:00
"io"
"net"
"os"
"github.com/pelletier/go-toml"
2026-09-10 22:31:23 -06:00
2026-10-05 15:43:05 -06:00
"go.destrealm.net/cwrap"
"go.destrealm.net/vfs"
)
type Settings struct {
2026-10-05 15:43:05 -06:00
file string `toml:"-"`
FS vfs.VFS `toml:"-"`
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-10-16 00:14:24 -06:00
Debug *Debug `toml:"debug"`
}
2026-10-05 15:43:05 -06:00
// Reload previously loaded configuration.
func (s *Settings) Reload() error {
st, err := LoadSettings(s.file, s.FS)
2026-10-05 15:43:05 -06:00
if err != nil {
return err
}
s.API = st.API
s.DailyScripture = st.DailyScripture
s.Translations = st.Translations
s.System = st.System
s.Service = st.Service
s.Chat = st.Chat
return nil
}
func (s *Settings) Save() error {
buf, err := s.Marshal()
if err != nil {
return err
}
fp, err := s.FS.Mutable().OpenFile(s.file, os.O_TRUNC|os.O_CREATE, 0644)
2026-10-05 15:43:05 -06:00
if err != nil {
return err
}
defer fp.Close()
_, err = fp.Write(buf)
return err
}
type API struct {
2026-09-12 15:41:20 -06:00
BaseURL string `toml:"base_url"`
}
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
}
type DailyScripture struct {
2026-09-12 15:41:20 -06:00
UserSubmitted bool `toml:"user_submitted"`
Schedule string `toml:"schedule"`
}
type System struct {
2026-10-16 00:14:24 -06:00
TimeZone string `toml:"timezone"`
DSN string `toml:"dsn"`
Logging *Logging `toml:"logging"`
}
type Logging struct {
Level string `toml:"level"`
}
type Service struct {
2026-10-16 00:14:24 -06:00
BotToken string `toml:"secret"`
Method string `toml:"method"`
Webhook *Webhook `toml:"webhook"`
}
type Webhook struct {
2026-09-12 15:41:20 -06:00
URI string `toml:"uri"`
Port int `toml:"port"`
Host string `toml:"host"`
}
func (w *Webhook) ListenAddr() string {
return net.JoinHostPort(w.Host, fmt.Sprintf("%d", w.Port))
}
type Translations struct {
BaseDir string `toml:"basedir"`
Current string `toml:"current"`
Active []string `toml:"active"`
2026-10-23 11:09:01 -06:00
Default string `toml:"default"`
}
2026-10-16 00:14:24 -06:00
type Debug struct {
Commands struct {
2026-10-21 19:44:38 -06:00
// Register bot commands with Telegram. If false and `Reset` is true,
// commands will be removed and not re-added.
Register bool `toml:"register"`
2026-10-16 00:14:24 -06:00
// Resets the command list by deleting all existing commands.
Reset bool `toml:"reset"`
} `toml:"commands"`
}
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-10-05 15:43:05 -06:00
func LoadSettings(name string, fs vfs.VFS) (*Settings, error) {
2026-10-23 09:07:39 -06:00
conf := &Settings{
API: &API{},
Chat: &Chat{
Channels: make([]int64, 0),
},
DailyScripture: &DailyScripture{
UserSubmitted: false,
Schedule: "0 8 * * *",
},
System: &System{
TimeZone: "UTC",
DSN: "",
Logging: &Logging{
Level: "info",
},
},
Service: &Service{
BotToken: "",
Method: "polling",
Webhook: &Webhook{
URI: "",
Port: 0,
Host: "localhost",
},
},
2026-10-23 11:09:01 -06:00
Translations: &Translations{
BaseDir: "dat/translations",
Current: "bsb",
Active: []string{"bsb", "kjv"},
Default: "bsb",
},
2026-10-23 09:07:39 -06:00
}
2026-10-05 15:43:05 -06:00
file, err := fs.Open(name)
if err != nil {
return nil, err
}
defer file.Close()
2026-10-05 15:43:05 -06:00
buf, err := io.ReadAll(file)
if err != nil {
return nil, ErrReadingSettings.Do(err)
}
err = toml.Unmarshal(buf, conf)
if err != nil {
return nil, ErrReadingSettings.Do(err)
}
2026-10-05 15:43:05 -06:00
conf.file = name
return conf, nil
}
2026-09-10 22:31:23 -06:00
func SaveSettings(name string, settings *Settings) error {
2026-10-05 15:43:05 -06:00
return settings.Save()
2026-09-10 22:31:23 -06:00
}