From d75a02554de51808567cbf4389b4d5b6342f7efd Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Mon, 22 Aug 2022 19:29:42 -0500 Subject: [PATCH] Bind Discord and OpenWeatherMap tokens to environment variables --- cmd/bb/main.go | 14 ++++---------- command/config.go | 6 ++++-- docker-compose.yaml | 1 + 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/cmd/bb/main.go b/cmd/bb/main.go index e600f72..558923a 100644 --- a/cmd/bb/main.go +++ b/cmd/bb/main.go @@ -22,8 +22,6 @@ import ( ) var ( - Token string - defaultReactions []string = []string{"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", "🔥", "🍒", "🎉", "🥳", "🎊"} C command.Config @@ -62,27 +60,23 @@ func main() { 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) } - Token, ok := viper.Get("discord_token").(string) - err = viper.Unmarshal(&C) if err != nil { log.Fatalf("unable to decode into struct: %v", err) } - if Token == "" { + if C.DiscordToken == "" { log.Fatalf("Discord token is not set") } - if !ok { - log.Fatalf("Invalid type assertion") - } - - dg, err := discordgo.New(fmt.Sprintf("Bot %s", Token)) + dg, err := discordgo.New(fmt.Sprintf("Bot %s", C.DiscordToken)) if err != nil { log.Fatalf("error creating Discord session: %v\n", err) } diff --git a/command/config.go b/command/config.go index f2ef0f5..55105f4 100644 --- a/command/config.go +++ b/command/config.go @@ -10,8 +10,10 @@ var ( type ( Config struct { - Handler HandlerConfig `mapstructure:"handler"` - Prefix string `mapstructure:"prefix"` + Handler HandlerConfig `mapstructure:"handler"` + Prefix string `mapstructure:"prefix"` + DiscordToken string `mapstructure:"discord_token"` + OpenWeatherMapToken string `mapstructure:"open_weather_map_token"` } HandlerConfig struct { diff --git a/docker-compose.yaml b/docker-compose.yaml index 9d004eb..6b8beec 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,3 +4,4 @@ services: build: . environment: - BEEPBOOP_DISCORD_TOKEN + - BEEPBOOP_OPEN_WEATHER_MAP_TOKEN