From 5f5c67f7ccce5e1e7c988172203bdca4f2f27502 Mon Sep 17 00:00:00 2001 From: Hunman Date: Mon, 2 Jan 2023 09:05:23 +0000 Subject: [PATCH] Warn when Koanf read fails (#47) Figured out the problem in issue #32, the toml file syntax was invalid (I had `'` instead of `"` at some values), but there was nothing in the logs about it. Moved the config reading (and watching) into the same function, which logs the error as a warning. I wanted to try moving the whole `if` into a separate function, but I kept getting errors when I tried to extract the `path` from the `File`, so I have that attempt in a separate branch not in this pull request: https://gitea.arsenm.dev/Hunman/itd/commit/5a84bf81489d3dc57f197f5feef5521950645ba5 Reviewed-on: https://gitea.arsenm.dev/Arsen6331/itd/pulls/47 Co-authored-by: Hunman Co-committed-by: Hunman --- config.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/config.go b/config.go index 1d95a50..8956d4d 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}) @@ -51,15 +53,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,14 +63,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().Str("filename", filename).Err(cfgError).Msg("Error while trying to read config file") + } + // 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().Str("filename", filename).Err(cfgError).Msg("Error while trying to read config file") + } }) }