Compare commits

...

3 Commits

Author SHA1 Message Date
5a84bf8148 WIP: Attempting to move code duplication into another function
What is this language's problem?
2022-12-30 01:35:45 +01:00
a254762288 Load the configs in one function, which handles the error too
We are doing the same thing to global and user level config, so it makes
sense to do it in a separate function
Loaded in the fmt package, so we can print out everything in one useful
error (actually warning) message
2022-12-30 00:23:31 +01:00
82bb145cbc Move the global config path into a variable 2022-12-30 00:17:38 +01:00

View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
@ -16,6 +17,8 @@ import (
var cfgDir string
func init() {
etcPath := "/etc/itd.toml";
// Set up logger
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
@ -51,15 +54,9 @@ func init() {
// Set config defaults
setCfgDefaults()
// Load config files
etcProvider := file.Provider("/etc/itd.toml")
cfgProvider := file.Provider(cfgPath)
k.Load(etcProvider, toml.Parser())
k.Load(cfgProvider, toml.Parser())
// Watch configs for changes
cfgWatch(etcProvider)
cfgWatch(cfgProvider)
// Load and watch config files
loadAndwatchCfgFile(etcPath)
loadAndwatchCfgFile(cfgPath)
// Load envireonment variables
k.Load(env.Provider("ITD_", "_", func(s string) string {
@ -67,17 +64,31 @@ func init() {
}), nil)
}
func cfgWatch(provider *file.File) {
func loadAndwatchCfgFile(filename string) {
provider := file.Provider(filename)
loadCfg(provider)
// Watch for changes and reload when detected
provider.Watch(func(_ interface{}, err error) {
if err != nil {
return
}
k.Load(provider, toml.Parser())
loadCfg(provider)
})
}
func loadCfg(provider *file.File) {
fmt.Println("%#v\n", provider) // &file.File{path:"/etc/itd.toml"}
filename := ""
filename = provider.path // provider.path undefined (type *file.File has no field or method path)
filename = (*provider).path // (*provider).path undefined (type file.File has no field or method path)
if cfgError := k.Load(provider, toml.Parser()); cfgError != nil {
log.Warn().Msg(fmt.Sprintf("Error while trying to read %s: %s\n", filename, cfgError.Error()))
}
}
func setCfgDefaults() {
k.Load(confmap.Provider(map[string]interface{}{
"bluetooth.adapter": "hci0",