forked from mmcdole/heyemoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
62 lines (50 loc) · 1.29 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
package main
import (
"strconv"
"strings"
"github.com/spf13/viper"
)
type Config struct {
BotName string
DatabasePath string
SlackToken string
SlackEmoji string
SlackEmojiMap map[string]int
SlackDailyCap int
WebSocketPort int
}
func readConfig() *Config {
viper.SetEnvPrefix("hey")
viper.AutomaticEnv()
viper.SetDefault("bot_name", "heyemoji")
viper.SetDefault("database_path", "./data/")
viper.SetDefault("slack_api_token", "")
viper.SetDefault("slack_emoji", "star:1")
viper.SetDefault("slack_daily_cap", 5)
viper.SetDefault("websocket_port", 3334)
c := &Config{
BotName: viper.GetString("bot_name"),
DatabasePath: viper.GetString("database_path"),
SlackToken: viper.GetString("slack_token"),
SlackEmoji: viper.GetString("slack_emoji"),
SlackDailyCap: viper.GetInt("slack_daily_cap"),
WebSocketPort: viper.GetInt("websocket_port"),
}
c.SlackEmojiMap = createEmojiValueMap(c.SlackEmoji)
return c
}
func createEmojiValueMap(e string) map[string]int {
pairs := strings.Split(e, ",")
evalues := make(map[string]int)
for _, pair := range pairs {
evalue := strings.Split(pair, ":")
if len(evalue) != 2 {
continue
}
emoji := evalue[0]
if karma, err := strconv.Atoi(evalue[1]); err == nil {
evalues[emoji] = karma
}
}
return evalues
}