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"
|
|
|
|
)
|
|
|
|
|
2022-05-11 01:03:37 +00:00
|
|
|
var cfgDir string
|
|
|
|
|
2021-08-24 15:54:08 +00:00
|
|
|
func init() {
|
2022-12-29 23:17:38 +00:00
|
|
|
etcPath := "/etc/itd.toml";
|
|
|
|
|
2021-08-24 15:54:08 +00:00
|
|
|
// 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
|
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
|
2022-11-25 01:36:25 +00:00
|
|
|
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
|
|
|
|
2022-12-29 23:23:31 +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)
|
|
|
|
}
|
|
|
|
|
2022-12-29 23:23:31 +00:00
|
|
|
func loadAndwatchCfgFile(filename string) {
|
|
|
|
provider := file.Provider(filename)
|
|
|
|
|
|
|
|
if cfgError := k.Load(provider, toml.Parser()); cfgError != nil {
|
2023-01-02 09:00:23 +00:00
|
|
|
log.Warn().Str("filename", filename).Err(cfgError).Msg("Error while trying to read config file")
|
2022-12-29 23:23:31 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-29 23:23:31 +00:00
|
|
|
if cfgError := k.Load(provider, toml.Parser()); cfgError != nil {
|
2023-01-02 09:00:23 +00:00
|
|
|
log.Warn().Str("filename", filename).Err(cfgError).Msg("Error while trying to read config file")
|
2022-12-29 23:23:31 +00:00
|
|
|
}
|
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{},
|
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
|
|
|
}
|