diff --git a/cmd/bb/main.go b/cmd/bb/main.go index 709ef5b..ebd05e1 100644 --- a/cmd/bb/main.go +++ b/cmd/bb/main.go @@ -37,41 +37,10 @@ var ( ) func main() { - var ( - err error - ) - - C = bot.NewConfig() - - flag.Bool("debug", false, "enable debug logging") - pflag.CommandLine.AddGoFlagSet(flag.CommandLine) - pflag.Parse() - viper.BindPFlags(pflag.CommandLine) - - if viper.GetBool("debug") { - log.SetLevel(log.DebugLevel) - } + setupConfig() lib.SeedMathRand() - viper.SetEnvPrefix("BEEPBOOP") - viper.AutomaticEnv() - viper.SetConfigName("config") - viper.SetConfigType("toml") - viper.AddConfigPath(".") - err = viper.ReadInConfig() - viper.BindEnv("DISCORD_TOKEN") - viper.BindEnv("OPEN_WEATHER_MAP_TOKEN") - - if _, ok := err.(viper.ConfigFileNotFoundError); !ok { - log.Fatalf("fatal error config file: %v", err) - } - - err = viper.Unmarshal(&C) - if err != nil { - log.Fatalf("unable to decode into struct: %v", err) - } - if C.DiscordToken == "" { log.Fatalf("Discord token is not set") } @@ -103,3 +72,39 @@ func main() { log.Info("Shutting down") } + +func setupConfig() { + var err error + + C = bot.NewConfig() + + flag.Bool("debug", false, "enable debug logging") + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) + pflag.Parse() + viper.BindPFlags(pflag.CommandLine) + + if viper.GetBool("debug") { + log.SetLevel(log.DebugLevel) + } + + viper.SetEnvPrefix("BEEPBOOP") + viper.AutomaticEnv() + + viper.SetConfigName("config") + viper.SetConfigType("toml") + viper.AddConfigPath(".") + + err = viper.ReadInConfig() + + viper.BindEnv("DISCORD_TOKEN") + viper.BindEnv("OPEN_WEATHER_MAP_TOKEN") + + if _, ok := err.(viper.ConfigFileNotFoundError); !ok { + log.Fatalf("fatal error config file: %v", err) + } + + err = viper.Unmarshal(&C) + if err != nil { + log.Fatalf("unable to decode into struct: %v", err) + } +}