From 7a0c90a5f75976114ce5c4e78ee36f36fa502bcb Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Thu, 22 Sep 2022 22:53:40 -0500 Subject: [PATCH] Move config to a separate package --- bot/bot.go | 9 ++++---- bot/command.go | 3 ++- bot/config.go | 40 --------------------------------- config/config.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 45 deletions(-) delete mode 100644 bot/config.go create mode 100644 config/config.go diff --git a/bot/bot.go b/bot/bot.go index 0ef1540..d17d42d 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -7,6 +7,7 @@ import ( "os/signal" "syscall" + "git.kill0.net/chill9/beepboop/config" "git.kill0.net/chill9/beepboop/lib" "github.com/bwmarrin/discordgo" log "github.com/sirupsen/logrus" @@ -14,12 +15,12 @@ import ( "github.com/spf13/viper" ) -var C Config +var C *config.Config type ( Bot struct { Session *discordgo.Session - Config Config + Config *config.Config } MessageHandler func(s *discordgo.Session, m *discordgo.MessageCreate) @@ -32,7 +33,7 @@ func init() { viper.BindPFlags(pflag.CommandLine) } -func NewBot(s *discordgo.Session, config Config) *Bot { +func NewBot(s *discordgo.Session, config *config.Config) *Bot { return &Bot{Session: s, Config: config} } @@ -127,7 +128,7 @@ func Run() error { } func initConfig() { - C = NewConfig() + C = config.NewConfig() viper.SetEnvPrefix("BEEPBOOP") viper.AutomaticEnv() diff --git a/bot/command.go b/bot/command.go index f9cf256..7ea1831 100644 --- a/bot/command.go +++ b/bot/command.go @@ -3,6 +3,7 @@ package bot import ( "fmt" + "git.kill0.net/chill9/beepboop/config" "git.kill0.net/chill9/beepboop/lib" "github.com/bwmarrin/discordgo" log "github.com/sirupsen/logrus" @@ -19,7 +20,7 @@ type ( Command struct { Name string - Config Config + Config *config.Config Func CommandFunc NArgs int } diff --git a/bot/config.go b/bot/config.go deleted file mode 100644 index 4ccadf5..0000000 --- a/bot/config.go +++ /dev/null @@ -1,40 +0,0 @@ -package bot - -const ( - defaultPrefix = "!" -) - -var ( - defaultReactions []string = []string{ - "👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", - "🔥", "🍒", "🎉", "🥳", "🎊", "📉", "📈", "💀", "☠️", - } -) - -type ( - Config struct { - Debug bool `mapstructure:"debug"` - Handler HandlerConfig `mapstructure:"handler"` - Prefix string `mapstructure:"prefix"` - DiscordToken string `mapstructure:"discord_token"` - OpenWeatherMapToken string `mapstructure:"open_weather_map_token"` - } - - HandlerConfig struct { - Reaction ReactionConfig `mapstructure:"reaction"` - } - - ReactionConfig struct { - Emojis []string - Channels []string - } -) - -func NewConfig() Config { - var c Config - - c.Prefix = defaultPrefix - c.Handler.Reaction.Emojis = defaultReactions - - return c -} diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..c955498 --- /dev/null +++ b/config/config.go @@ -0,0 +1,58 @@ +package config + +const ( + defaultPrefix = "!" +) + +var ( + defaultReactions []string = []string{ + "👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", + "🔥", "🍒", "🎉", "🥳", "🎊", "📉", "📈", "💀", "☠️", + } +) + +type ( + Config struct { + Debug bool `mapstructure:"debug"` + Handler HandlerConfig `mapstructure:"handler"` + Prefix string `mapstructure:"prefix"` + DiscordToken string `mapstructure:"discord_token"` + OpenWeatherMapToken string `mapstructure:"open_weather_map_token"` + Mongo MongoConfig `mapstructure:"mongo"` + Redis RedisConfig `mapstructure:"redis"` + Postgres PostgresConfig `mapstructure:"postgres"` + } + + HandlerConfig struct { + Reaction ReactionConfig `mapstructure:"reaction"` + } + + ReactionConfig struct { + Emojis []string + Channels []string + } + + MongoConfig struct { + Uri string `mapstructure:"uri"` + Database string `mapstructure:"database"` + } + + RedisConfig struct { + Addr string `mapstructure:"addr"` + Password string `mapstructure:"password"` + DB int `mapstructure:"database"` + } + + PostgresConfig struct { + Uri string `mapstructure:"uri"` + } +) + +func NewConfig() *Config { + var c *Config = &Config{} + + c.Prefix = defaultPrefix + c.Handler.Reaction.Emojis = defaultReactions + + return c +}