-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
81 lines (69 loc) · 1.68 KB
/
utils.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
package coinfactory
import (
"strings"
"github.com/fsnotify/fsnotify"
"github.com/sinisterminister/coinfactory/pkg/binance"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func filterSymbols(symbols []string) []string {
filter := func(symbol string) bool {
sym := viper.GetStringSlice("watchedSymbols")
c := 0
for _, s := range sym {
if strings.Contains(symbol, s) {
c++
}
}
return c >= 2
}
filtered := []string{}
for _, s := range symbols {
if filter(s) {
filtered = append(filtered, s)
}
}
return filtered
}
func fetchWatchedSymbols() []string {
return filterSymbols(binance.GetSymbolsAsStrings())
}
func init() {
// Setup the paths where Viper will search for the config file
viper.SetConfigName("." + appName)
viper.AddConfigPath("/etc/" + appName)
viper.AddConfigPath("$HOME/." + appName)
viper.AddConfigPath("$HOME")
// Set the defaults
setConfigDefaults()
// Read in the config
log.Info("Loading configuration file...")
err := viper.ReadInConfig()
// If we can't read the config file, we can't do anything. Bail out!
if err != nil {
log.Panic("Could not load config file! ", err)
}
setLogLevelFromConfig()
log.Debug("Configuration file values: ", viper.GetViper)
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
setLogLevelFromConfig()
})
}
func setConfigDefaults() {
viper.SetDefault("orderUpdateInterval", 15)
}
func setLogLevelFromConfig() {
switch viper.GetString("logLevel") {
case "DEBUG":
log.SetLevel(log.DebugLevel)
case "INFO":
log.SetLevel(log.InfoLevel)
case "WARN":
log.SetLevel(log.WarnLevel)
case "ERROR":
log.SetLevel(log.ErrorLevel)
default:
log.SetLevel(log.InfoLevel)
}
}