-
Notifications
You must be signed in to change notification settings - Fork 0
/
holder.go
52 lines (42 loc) 路 990 Bytes
/
holder.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
package config
import (
"log/slog"
"os"
"github.com/ghodss/yaml"
"github.com/joho/godotenv"
env "github.com/qiangxue/go-env"
)
var Config = new(BotConfig)
const (
yamlFilePath = "config.yml"
envFilePath = ".env"
envPrefix = "BOT_"
)
func Load() error {
if _, err := os.Stat(yamlFilePath); err == nil {
slog.Info("Yaml file located, loading", "path", yamlFilePath)
err = loadConfigFromYaml()
if err == nil {
slog.Info("Config loaded")
}
return err
}
slog.Warn("Yaml config file not found, using environment...")
if _, err := os.Stat(envFilePath); err == nil {
slog.Info("Found env file, loading...", "path", envFilePath)
err := godotenv.Load(envFilePath)
if err != nil {
slog.Error("Cannot load env file", err)
os.Exit(1)
}
}
loader := env.New(envPrefix, nil)
return loader.Load(Config)
}
func loadConfigFromYaml() error {
file, err := os.ReadFile(yamlFilePath)
if err != nil {
return err
}
return yaml.Unmarshal(file, Config)
}