config parse doesnt fatal in default
All checks were successful
continuous-integration/drone/push Build is passing

when starting schnutibox a parsing error would fatal and exit the
program. when config reloading it just errors and does nothing.
This commit is contained in:
Marvin Steadfast 2021-04-18 11:33:15 +02:00
parent 2ad0221259
commit 979040227e

View File

@ -75,7 +75,7 @@ func initConfig() {
// Parse config file. // Parse config file.
if cfgFile != "" { if cfgFile != "" {
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
parseConfig(logger) parseConfig(logger, true)
} else { } else {
logger.Fatal().Msg("missing config file") logger.Fatal().Msg("missing config file")
} }
@ -83,22 +83,42 @@ func initConfig() {
viper.WatchConfig() viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) { viper.OnConfigChange(func(e fsnotify.Event) {
logger.Info().Msg("config file changed") logger.Info().Msg("config file changed")
parseConfig(logger) parseConfig(logger, false)
}) })
} }
func parseConfig(logger zerolog.Logger) { // parseConfig parses the config and does some tests if required fields are there.
// Its also possible to decide if parsing should end up in a fatal or just an error.
func parseConfig(logger zerolog.Logger, fatal bool) {
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
if fatal {
logger.Fatal().Err(err).Msg("error loading config file") logger.Fatal().Err(err).Msg("error loading config file")
} }
logger.Error().Err(err).Msg("error loading config file")
return
}
if err := viper.Unmarshal(&config.Cfg); err != nil { if err := viper.Unmarshal(&config.Cfg); err != nil {
if fatal {
logger.Fatal().Err(err).Msg("could not unmarshal config") logger.Fatal().Err(err).Msg("could not unmarshal config")
} }
logger.Error().Err(err).Msg("could not unmarshal config")
return
}
if err := config.Cfg.Require(); err != nil { if err := config.Cfg.Require(); err != nil {
if fatal {
logger.Fatal().Err(err).Msg("missing config parts") logger.Fatal().Err(err).Msg("missing config parts")
} }
logger.Error().Err(err).Msg("missing config parts")
return
}
} }
// Execute executes the commandline interface. // Execute executes the commandline interface.