-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
117 lines (101 loc) · 2.76 KB
/
config.go
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
package gokord
import (
_ "embed"
"errors"
"fmt"
"github.com/anhgelus/gokord/utils"
"github.com/pelletier/go-toml/v2"
"log/slog"
"os"
)
var (
//go:embed resources/config.toml
DefaultBaseConfig string
// BaseCfg is the BaseConfig used by the bot
BaseCfg BaseConfig
ErrImpossibleToConnectDB = errors.New("impossible to connect to the database")
ErrImpossibleToConnectRedis = errors.New("impossible to connect to redis")
)
const (
ConfigFolder = "config"
)
// BaseConfig is all basic configuration (debug, redis connection and database connection)
type BaseConfig struct {
Debug bool `toml:"debug"`
Author string `toml:"author"`
Redis RedisCredentials `toml:"redis"`
Database DatabaseCredentials `toml:"database"`
}
type RedisCredentials struct {
Address string `toml:"address"`
Password string `toml:"password"`
DB int `toml:"db"`
}
type DatabaseCredentials struct {
Host string `toml:"host"`
User string `toml:"user"`
Password string `toml:"password"`
DBName string `toml:"db_name"`
Port int `toml:"port"`
}
// ConfigInfo has all required information to get a config
type ConfigInfo struct {
Cfg any // pointer to the struct
Name string // name of the config
Default string // default content of the config
}
// getBaseConfig get the BaseConfig
func getBaseConfig(cfg any, defaultConfig string) error {
return Get(cfg, defaultConfig, "config")
}
// Get a config (already called on start)
func Get(cfg any, defaultConfig string, name string) error {
path := fmt.Sprintf("%s/%s.toml", ConfigFolder, name)
err := os.Mkdir(ConfigFolder, 0666)
if err != nil && !os.IsExist(err) {
return err
}
c, err := os.ReadFile(path)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
utils.SendAlert("config.go - Create file", "File not found, creating a new one.")
c = []byte(defaultConfig)
err = os.WriteFile(path, c, 0666)
if err != nil {
return err
}
}
return toml.Unmarshal(c, cfg)
}
// SetupConfigs with the given configs (+ base config which is available at BaseCfg)
func SetupConfigs(cfgInfo []*ConfigInfo) error {
err := getBaseConfig(&BaseCfg, DefaultBaseConfig)
if err != nil {
return err
}
Debug = BaseCfg.Debug
utils.Author = BaseCfg.Author
if Debug {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
for _, cfg := range cfgInfo {
err = Get(cfg.Cfg, cfg.Name, cfg.Default)
if err != nil {
return err
}
}
DB, err = BaseCfg.Database.Connect()
if err != nil {
utils.SendAlert("config.go - connection to database", err.Error())
return ErrImpossibleToConnectDB
}
c, err := BaseCfg.Redis.Get()
if err != nil {
utils.SendAlert("config.go - connection to redis", err.Error())
return ErrImpossibleToConnectRedis
}
_ = c.Close()
return nil
}