adds config watching and config reloading

This commit is contained in:
Marvin Steadfast 2021-04-02 08:59:21 +02:00
parent ecadf780ea
commit 2b536a0a8a

View File

@ -6,6 +6,8 @@ import (
"os" "os"
"strings" "strings"
"github.com/fsnotify/fsnotify"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -26,39 +28,57 @@ var rootCmd = &cobra.Command{
// init initializes the command line interface. // init initializes the command line interface.
func init() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
rootCmd.AddCommand(runCmd)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file") rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file")
// Defaults // Run.
viper.SetDefault("box.hostname", "localhost") rootCmd.AddCommand(runCmd)
viper.SetDefault("box.port", 9999)
viper.SetDefault("mpd.hostname", "localhost") // Prepare.
viper.SetDefault("mpd.port", 6600) rootCmd.AddCommand(prepareCmd)
viper.SetDefault("reader.dev", "/dev/hidraw0")
} }
// initConfig loads the config file. // initConfig loads the config file.
// TODO: needs some environment variable love! // TODO: needs some environment variable love!
func initConfig() { func initConfig() {
logger := log.With().Str("config", cfgFile).Logger() logger := log.With().Str("config", cfgFile).Logger()
// Defaults.
viper.SetDefault("box.hostname", "localhost")
viper.SetDefault("box.port", 9999)
viper.SetDefault("mpd.hostname", "localhost")
viper.SetDefault("mpd.port", 6600)
viper.SetDefault("reader.dev", "/dev/hidraw0")
// Environment handling.
viper.SetEnvPrefix("SCHNUTIBOX") viper.SetEnvPrefix("SCHNUTIBOX")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv() viper.AutomaticEnv()
// Parse config file.
if cfgFile != "" { if cfgFile != "" {
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
if err := viper.ReadInConfig(); err != nil { parseConfig(logger)
logger.Fatal().Err(err).Msg("error loading config file")
}
if err := viper.Unmarshal(&config.Cfg); err != nil {
logger.Fatal().Err(err).Msg("could not unmarshal config")
}
if err := config.Cfg.Require(); err != nil {
logger.Fatal().Err(err).Msg("missing config parts")
}
} else { } else {
logger.Fatal().Msg("missing config file") logger.Fatal().Msg("missing config file")
} }
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
logger.Info().Msg("config file changed")
parseConfig(logger)
})
}
func parseConfig(logger zerolog.Logger) {
if err := viper.ReadInConfig(); err != nil {
logger.Fatal().Err(err).Msg("error loading config file")
}
if err := viper.Unmarshal(&config.Cfg); err != nil {
logger.Fatal().Err(err).Msg("could not unmarshal config")
}
if err := config.Cfg.Require(); err != nil {
logger.Fatal().Err(err).Msg("missing config parts")
}
} }
// Execute executes the commandline interface. // Execute executes the commandline interface.