From adc50afd70f13a8956a2ceac0c3c76a1b19d7f31 Mon Sep 17 00:00:00 2001 From: Joey Wilhelm Date: Wed, 25 Oct 2023 11:46:50 -0600 Subject: [PATCH] fix: Don't clobber file config when loading env --- config/config.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index 1777d6a..0e3a622 100644 --- a/config/config.go +++ b/config/config.go @@ -58,14 +58,15 @@ func GetUserConfig(configFile string) (Config, error) { } viper.SetConfigFile(configFile) if err := viper.ReadInConfig(); err != nil { - log.Fatal().Err(err).Msg("Error reading config file.") + log.Error().Err(err).Msg("Error reading config file.") + return Config{}, err } // (Optionally) Load a .env file if fileExists("./.env") { viper.SetConfigFile("./.env") viper.SetConfigType("env") - if err := viper.ReadInConfig(); err != nil { + if err := viper.MergeInConfig(); err != nil { log.Error().Err(err).Msg("Error loading .env file.") } } else { @@ -78,8 +79,9 @@ func GetUserConfig(configFile string) (Config, error) { viper.SetEnvPrefix("vulnbot") viper.AutomaticEnv() - // Finally, copy all loaded values into the config object - _ = viper.Unmarshal(&userCfg) + if err := viper.Unmarshal(&userCfg); err != nil { + return Config{}, err + } return userCfg, nil }