From 6c0000d409d79887353039c2dd7c2d3617c707bc Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Wed, 24 Aug 2022 08:52:30 -0500 Subject: [PATCH] Tidy up main --- cmd/bb/main.go | 69 +++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 32 deletions(-) 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) + } +}