diff options
| author | Bobby <[email protected]> | 2024-11-16 16:21:51 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2024-11-16 16:21:51 -0500 |
| commit | 2e79b5aa8cfe8433bcd9d1d415826056fea8fcb2 (patch) | |
| tree | e9a8e4fe1f8dfc598f68f3a2c1909fa381f46961 /config | |
| parent | c7e9ca772b03c2edc3a1893e9c9ac2eeba90acfa (diff) | |
| download | yuzaki-2e79b5aa8cfe8433bcd9d1d415826056fea8fcb2.tar.xz yuzaki-2e79b5aa8cfe8433bcd9d1d415826056fea8fcb2.zip | |
bot setup with basic message handler
Diffstat (limited to 'config')
| -rw-r--r-- | config/config.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..4f7a666 --- /dev/null +++ b/config/config.go @@ -0,0 +1,92 @@ +package config + +import ( + "fmt" + "os" + "strings" + + "github.com/joho/godotenv" +) + +type Config struct { + DiscordToken string + DataSourceName string + DatabaseDriver string +} + +var validDBDrivers = map[string]bool{ + "sqlite": true, + "postgres": true, + "mysql": true, + "mssql": true, +} + +func Load() (*Config, error) { + if err := godotenv.Load(); err != nil { + return nil, fmt.Errorf("loading env file: %w", err) + } + + config := &Config{} + requiredVars := map[string]*string{ + "DISCORD_BOT_TOKEN": &config.DiscordToken, + "DSN": &config.DataSourceName, + "DATABASE_DRIVER": &config.DatabaseDriver, + } + + for envKey, configVar := range requiredVars { + value, err := getEnv(envKey) + if err != nil { + return nil, fmt.Errorf("getting %s: %w", envKey, err) + } + *configVar = value + } + + if err := validateConfig(config); err != nil { + return nil, fmt.Errorf("validating config: %w", err) + } + + return config, nil +} + +func validateConfig(config *Config) error { + if config.DiscordToken == "" { + return fmt.Errorf("discord token not found") + } + if config.DataSourceName == "" { + return fmt.Errorf("data source name not found") + } + if config.DatabaseDriver == "" { + return fmt.Errorf("database driver not found") + } + + if !validDBDrivers[strings.ToLower(config.DatabaseDriver)] { + return fmt.Errorf("invalid database driver: %s", config.DatabaseDriver) + } + + return nil +} + +func getEnv(key string) (string, error) { + value, exists := os.LookupEnv(key) + if !exists { + return "", fmt.Errorf("environment variable %s not found", key) + } + return strings.TrimSpace(value), nil +} + +// Future implementation for getting other types of environment variables +// func getEnvInt(key string) (int, error) { +// value, err := getEnv(key) +// if err != nil { +// return 0, err +// } +// return strconv.Atoi(value) +// } + +// func getEnvBool(key string) (bool, error) { +// value, err := getEnv(key) +// if err != nil { +// return false, err +// } +// return strconv.ParseBool(value) +// } |
