From 5a84bf81489d3dc57f197f5feef5521950645ba5 Mon Sep 17 00:00:00 2001 From: Hunman Date: Fri, 30 Dec 2022 01:35:45 +0100 Subject: [PATCH] WIP: Attempting to move code duplication into another function What is this language's problem? --- config.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index c069349..379799f 100644 --- a/config.go +++ b/config.go @@ -67,9 +67,7 @@ func init() { 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())) - } + loadCfg(provider) // Watch for changes and reload when detected provider.Watch(func(_ interface{}, err error) { @@ -77,12 +75,20 @@ func loadAndwatchCfgFile(filename string) { return } - 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())) - } + 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",