-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
67 lines (59 loc) · 1.84 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
package config
import (
"strings"
"github.com/Strum355/log"
"github.com/spf13/viper"
)
// Servers represents the servers.
type Servers struct {
PublicServer string `json:"public"`
CommitteeServer string `json:"committee"`
}
// Channels required for events.
type Channels struct {
PublicAnnouncements string `json:"public_announcements"` // On public server
PublicGeneral string `json:"public_general"` // On public server
PrivateEvents string `json:"private_events"` // On committee server
}
const limitChars = 8
// InitConfig sets up viper and consul.
func InitConfig() error {
// Viper
initDefaults()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) // For gamers only
viper.AutomaticEnv()
viper.Set(
"discord.servers",
&Servers{PublicServer: viper.GetString("discord.public.server"), CommitteeServer: viper.GetString("discord.committee.server")},
)
viper.Set(
"discord.channels",
&Channels{PublicAnnouncements: viper.GetString("discord.public.channel"), PrivateEvents: viper.GetString("discord.committee.channel"), PublicGeneral: viper.GetString("discord.public.general")},
)
welcomeMessages := []string{}
for _, message := range strings.Split(viper.GetString("discord.public.welcome"), ",") {
welcomeMessages = append(welcomeMessages, message)
}
viper.Set("discord.welcome_messages", &welcomeMessages)
printAll()
return nil
}
func printAll() {
store := log.Fields{}
for k, v := range viper.AllSettings() {
localStore := map[string]interface{}{}
for sk, sv := range v.(map[string]interface{}) {
if strval, ok := sv.(string); ok {
if len(strval) > limitChars {
localStore[sk] = strval[:limitChars] + "..."
} else {
localStore[sk] = strval
}
} else {
localStore[sk] = sv
}
}
store[k] = localStore
}
log.WithFields(store).Info("discord bot startup config values")
}