Move config to a separate package

This commit is contained in:
Ryan Cavicchioni 2022-09-22 22:53:40 -05:00
parent b8b994157e
commit 7a0c90a5f7
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
4 changed files with 65 additions and 45 deletions

View File

@ -7,6 +7,7 @@ import (
"os/signal" "os/signal"
"syscall" "syscall"
"git.kill0.net/chill9/beepboop/config"
"git.kill0.net/chill9/beepboop/lib" "git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -14,12 +15,12 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var C Config var C *config.Config
type ( type (
Bot struct { Bot struct {
Session *discordgo.Session Session *discordgo.Session
Config Config Config *config.Config
} }
MessageHandler func(s *discordgo.Session, m *discordgo.MessageCreate) MessageHandler func(s *discordgo.Session, m *discordgo.MessageCreate)
@ -32,7 +33,7 @@ func init() {
viper.BindPFlags(pflag.CommandLine) 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} return &Bot{Session: s, Config: config}
} }
@ -127,7 +128,7 @@ func Run() error {
} }
func initConfig() { func initConfig() {
C = NewConfig() C = config.NewConfig()
viper.SetEnvPrefix("BEEPBOOP") viper.SetEnvPrefix("BEEPBOOP")
viper.AutomaticEnv() viper.AutomaticEnv()

View File

@ -3,6 +3,7 @@ package bot
import ( import (
"fmt" "fmt"
"git.kill0.net/chill9/beepboop/config"
"git.kill0.net/chill9/beepboop/lib" "git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -19,7 +20,7 @@ type (
Command struct { Command struct {
Name string Name string
Config Config Config *config.Config
Func CommandFunc Func CommandFunc
NArgs int NArgs int
} }

View File

@ -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
}

58
config/config.go Normal file
View File

@ -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
}