Tidy up main

This commit is contained in:
Ryan Cavicchioni 2022-08-24 08:52:30 -05:00
parent 547063de2e
commit 6c0000d409
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D

View File

@ -37,41 +37,10 @@ var (
) )
func main() { func main() {
var ( setupConfig()
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)
}
lib.SeedMathRand() 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 == "" { if C.DiscordToken == "" {
log.Fatalf("Discord token is not set") log.Fatalf("Discord token is not set")
} }
@ -103,3 +72,39 @@ func main() {
log.Info("Shutting down") 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)
}
}