Socialify

Folder ..

Viewing config.go
124 lines (103 loc) • 2.5 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package config

import (
	"fmt"
	"os"
	"strings"

	"github.com/joho/godotenv"
)

var BotConfig *Config

type Config struct {
	DiscordToken       string
	DataSourceName     string
	DatabaseDriver     string
	ConfiguredChannels struct {
		PoketwoSpawns []string
	}
}

var validDBDrivers = map[string]bool{
	"sqlite":   true,
	"postgres": true,
	"mysql":    true,
	"mssql":    true,
}

func Load() error {
	if err := godotenv.Load(); err != nil {
		return fmt.Errorf("loading env file: %w", err)
	}

	config := &Config{}

	// Basic string variables
	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 fmt.Errorf("getting %s: %w", envKey, err)
		}
		*configVar = value
	}

	channels, err := getEnvStringSlice("POKETWO_CHANNELS")
	if err != nil {
		return fmt.Errorf("getting POKETWO_CHANNELS: %w", err)
	}
	config.ConfiguredChannels.PoketwoSpawns = channels

	if err := validateConfig(config); err != nil {
		return fmt.Errorf("validating config: %w", err)
	}

	BotConfig = config
	return 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
}

func getEnvStringSlice(key string) ([]string, error) {
	value, err := getEnv(key)
	if err != nil {
		return nil, err
	}

	if value == "" {
		return []string{}, nil
	}

	items := strings.Split(value, ",")
	// Trim spaces from each item
	for i, item := range items {
		items[i] = strings.TrimSpace(item)
	}
	return items, 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)
// }