itd/config.go

111 lines
2.5 KiB
Go
Raw Normal View History

2021-08-24 15:54:08 +00:00
package main
import (
"os"
2022-02-21 19:20:02 +00:00
"path/filepath"
2021-08-24 15:54:08 +00:00
"strings"
2022-02-21 19:20:02 +00:00
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"go.arsenm.dev/logger"
"go.arsenm.dev/logger/log"
2021-08-24 15:54:08 +00:00
)
2022-05-11 01:03:37 +00:00
var cfgDir string
2021-08-24 15:54:08 +00:00
func init() {
2023-01-03 17:18:57 +00:00
etcPath := "/etc/itd.toml"
2021-08-24 15:54:08 +00:00
// Set up logger
log.Logger = logger.NewPretty(os.Stderr)
2021-08-24 15:54:08 +00:00
2022-02-21 19:20:02 +00:00
// Get user's configuration directory
2022-05-11 01:03:37 +00:00
userCfgDir, err := os.UserConfigDir()
2022-02-21 19:20:02 +00:00
if err != nil {
panic(err)
}
2022-05-11 01:03:37 +00:00
cfgDir = filepath.Join(userCfgDir, "itd")
// If config dir is not readable
if _, err = os.ReadDir(cfgDir); err != nil {
// Create config dir with 700 permissions
err = os.MkdirAll(cfgDir, 0o700)
2022-05-11 01:03:37 +00:00
if err != nil {
panic(err)
}
}
// Get current and old config paths
cfgPath := filepath.Join(cfgDir, "itd.toml")
oldCfgPath := filepath.Join(userCfgDir, "itd.toml")
// If old config path exists
if _, err = os.Stat(oldCfgPath); err == nil {
// Move old config to new path
err = os.Rename(oldCfgPath, cfgPath)
if err != nil {
panic(err)
}
}
2022-02-21 19:20:02 +00:00
// Set config defaults
2021-08-24 15:54:08 +00:00
setCfgDefaults()
2022-02-21 19:20:02 +00:00
// Load and watch config files
loadAndwatchCfgFile(etcPath)
loadAndwatchCfgFile(cfgPath)
2022-02-21 19:20:02 +00:00
// Load envireonment variables
k.Load(env.Provider("ITD_", "_", func(s string) string {
return strings.ToLower(strings.TrimPrefix(s, "ITD_"))
}), nil)
}
func loadAndwatchCfgFile(filename string) {
provider := file.Provider(filename)
if cfgError := k.Load(provider, toml.Parser()); cfgError != nil {
log.Warn("Error while trying to read config file").Str("filename", filename).Err(cfgError).Send()
}
2022-02-21 19:20:02 +00:00
// Watch for changes and reload when detected
provider.Watch(func(_ interface{}, err error) {
if err != nil {
return
}
if cfgError := k.Load(provider, toml.Parser()); cfgError != nil {
log.Warn("Error while trying to read config file").Str("filename", filename).Err(cfgError).Send()
}
2022-02-21 19:20:02 +00:00
})
2021-08-24 15:54:08 +00:00
}
func setCfgDefaults() {
2022-02-21 19:20:02 +00:00
k.Load(confmap.Provider(map[string]interface{}{
2022-05-03 03:17:38 +00:00
"bluetooth.adapter": "hci0",
2022-02-21 19:20:02 +00:00
"socket.path": "/tmp/itd/socket",
2021-08-24 15:54:08 +00:00
2022-02-21 19:20:02 +00:00
"conn.reconnect": true,
2021-08-24 15:54:08 +00:00
2022-02-21 19:20:02 +00:00
"conn.whitelist.enabled": false,
"conn.whitelist.devices": []string{},
2022-02-21 19:20:02 +00:00
"on.connect.notify": true,
2021-08-24 15:54:08 +00:00
2022-02-21 19:20:02 +00:00
"on.reconnect.notify": true,
"on.reconnect.setTime": true,
2021-08-24 15:54:08 +00:00
2022-02-21 19:20:02 +00:00
"notifs.translit.use": []string{"eASCII"},
"notifs.translit.custom": []string{},
2022-02-21 19:20:02 +00:00
"notifs.ignore.sender": []string{},
"notifs.ignore.summary": []string{"InfiniTime"},
"notifs.ignore.body": []string{},
2021-08-24 15:54:08 +00:00
2022-02-21 19:20:02 +00:00
"music.vol.interval": 5,
}, "."), nil)
2021-08-24 15:54:08 +00:00
}