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"
|
2021-08-24 15:54:08 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Set up logger
|
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
|
|
|
2022-02-21 19:20:02 +00:00
|
|
|
// Get user's configuration directory
|
|
|
|
cfgDir, err := os.UserConfigDir()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set config defaults
|
2021-08-24 15:54:08 +00:00
|
|
|
setCfgDefaults()
|
2022-02-21 19:20:02 +00:00
|
|
|
|
|
|
|
// Load config files
|
|
|
|
etcProvider := file.Provider("/etc/itd.toml")
|
|
|
|
cfgProvider := file.Provider(filepath.Join(cfgDir, "itd.toml"))
|
|
|
|
k.Load(etcProvider, toml.Parser())
|
|
|
|
k.Load(cfgProvider, toml.Parser())
|
|
|
|
|
|
|
|
// Watch configs for changes
|
|
|
|
cfgWatch(etcProvider)
|
|
|
|
cfgWatch(cfgProvider)
|
|
|
|
|
|
|
|
// Load envireonment variables
|
|
|
|
k.Load(env.Provider("ITD_", "_", func(s string) string {
|
|
|
|
return strings.ToLower(strings.TrimPrefix(s, "ITD_"))
|
|
|
|
}), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cfgWatch(provider *file.File) {
|
|
|
|
// Watch for changes and reload when detected
|
|
|
|
provider.Watch(func(_ interface{}, err error) {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
k.Load(provider, toml.Parser())
|
|
|
|
})
|
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{}{
|
|
|
|
"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{},
|
2021-11-01 18:28:55 +00:00
|
|
|
|
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{},
|
2021-11-01 18:28:55 +00:00
|
|
|
|
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
|
|
|
}
|