Fix imports, rework using VFS.
This commit is contained in:
parent
b6e81e1930
commit
b3855d0cdd
2 changed files with 46 additions and 13 deletions
|
|
@ -1,7 +1,7 @@
|
|||
package settings
|
||||
|
||||
import (
|
||||
"git.destrealm.net/go/errors/v2"
|
||||
"go.destrealm.net/errors"
|
||||
)
|
||||
|
||||
var ErrSettingsNotFound = errors.NewError("no settings.toml found")
|
||||
|
|
|
|||
|
|
@ -3,16 +3,19 @@ package settings
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
|
||||
"git.destrealm.net/go/cwrap"
|
||||
"go.destrealm.net/cwrap"
|
||||
"go.destrealm.net/vfs"
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
file string `toml:"-"`
|
||||
fs vfs.VFS `toml:"-"`
|
||||
API *API `toml:"api"`
|
||||
DailyScripture *DailyScripture `toml:"daily_scripture"`
|
||||
Translations *Translations `toml:"translations"`
|
||||
|
|
@ -21,6 +24,37 @@ type Settings struct {
|
|||
Chat *Chat `toml:"chat"`
|
||||
}
|
||||
|
||||
// Reload previously loaded configuration.
|
||||
func (s *Settings) Reload() error {
|
||||
st, err := LoadSettings(s.file, s.fs)
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fp.Close()
|
||||
_, err = fp.Write(buf)
|
||||
return err
|
||||
}
|
||||
|
||||
type API struct {
|
||||
BaseURL string `toml:"base_url"`
|
||||
}
|
||||
|
|
@ -74,14 +108,16 @@ func (c *Settings) Marshal() ([]byte, error) {
|
|||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func LoadSettings(name string) (*Settings, error) {
|
||||
func LoadSettings(name string, fs vfs.VFS) (*Settings, error) {
|
||||
conf := &Settings{}
|
||||
file, err := fs.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
buf, err := os.ReadFile(name)
|
||||
buf, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
if _, ok := err.(*fs.PathError); ok {
|
||||
return nil, ErrSettingsNotFound.Do(err)
|
||||
}
|
||||
return nil, ErrReadingSettings.Do(err)
|
||||
}
|
||||
|
||||
|
|
@ -90,13 +126,10 @@ func LoadSettings(name string) (*Settings, error) {
|
|||
return nil, ErrReadingSettings.Do(err)
|
||||
}
|
||||
|
||||
conf.file = name
|
||||
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)
|
||||
return settings.Save()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue