From 82bb145cbc0f215f3c97a840f9b7345b4d84f3b2 Mon Sep 17 00:00:00 2001 From: Hunman Date: Fri, 30 Dec 2022 00:17:38 +0100 Subject: [PATCH 1/3] Move the global config path into a variable --- config.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 1d95a50..2375cb2 100644 --- a/config.go +++ b/config.go @@ -16,6 +16,8 @@ import ( var cfgDir string func init() { + etcPath := "/etc/itd.toml"; + // Set up logger log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) @@ -52,7 +54,7 @@ func init() { setCfgDefaults() // Load config files - etcProvider := file.Provider("/etc/itd.toml") + etcProvider := file.Provider(etcPath) cfgProvider := file.Provider(cfgPath) k.Load(etcProvider, toml.Parser()) k.Load(cfgProvider, toml.Parser()) -- 2.45.2 From a254762288317377c5f041c35a6ea5f9d76f034e Mon Sep 17 00:00:00 2001 From: Hunman Date: Fri, 30 Dec 2022 00:23:31 +0100 Subject: [PATCH 2/3] 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 --- config.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/config.go b/config.go index 2375cb2..c069349 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "path/filepath" "strings" @@ -53,15 +54,9 @@ func init() { // Set config defaults setCfgDefaults() - // Load config files - etcProvider := file.Provider(etcPath) - 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 { @@ -69,14 +64,22 @@ func init() { }), nil) } -func cfgWatch(provider *file.File) { +func loadAndwatchCfgFile(filename string) { + provider := file.Provider(filename) + + 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())) + } + // Watch for changes and reload when detected provider.Watch(func(_ interface{}, err error) { if err != nil { return } - k.Load(provider, toml.Parser()) + 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())) + } }) } -- 2.45.2 From 7e4c916f0e98a149e80a4c2a4b5123a204d72ca8 Mon Sep 17 00:00:00 2001 From: Hunman Date: Mon, 2 Jan 2023 10:00:23 +0100 Subject: [PATCH 3/3] Replacing format strings with magic --- config.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index c069349..8956d4d 100644 --- a/config.go +++ b/config.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os" "path/filepath" "strings" @@ -68,7 +67,7 @@ func loadAndwatchCfgFile(filename string) { provider := file.Provider(filename) 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())) + log.Warn().Str("filename", filename).Err(cfgError).Msg("Error while trying to read config file") } // Watch for changes and reload when detected @@ -78,7 +77,7 @@ func loadAndwatchCfgFile(filename string) { } 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())) + log.Warn().Str("filename", filename).Err(cfgError).Msg("Error while trying to read config file") } }) } -- 2.45.2