-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
67 lines (53 loc) · 1.52 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 (
"time"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
// Provider defines a set of read-only methods for accessing the application
// configuration params as defined in one of the config files.
type Provider interface {
ConfigFileUsed() string
Get(key string) interface{}
GetBool(key string) bool
GetDuration(key string) time.Duration
GetFloat64(key string) float64
GetInt(key string) int
GetInt64(key string) int64
GetSizeInBytes(key string) uint
GetString(key string) string
GetStringMap(key string) map[string]interface{}
GetStringMapString(key string) map[string]string
GetStringMapStringSlice(key string) map[string][]string
GetStringSlice(key string) []string
GetTime(key string) time.Time
InConfig(key string) bool
IsSet(key string) bool
UnmarshalKey(string, interface{}, ...viper.DecoderConfigOption) error
OnConfigChange(run func(in fsnotify.Event))
WatchConfig()
}
var defaultConfig *viper.Viper
// Config returns a default config providers
func Config() Provider {
return defaultConfig
}
// LoadConfigProvider returns a configured viper instance
func LoadConfigProvider(appName string) Provider {
return readViperConfig(appName)
}
func init() {
defaultConfig = readViperConfig("DISHOOK")
}
func readViperConfig(appName string) *viper.Viper {
v := viper.New()
v.SetDefault("guild_id", "")
v.SetConfigName("config")
v.SetConfigType("yaml")
v.AddConfigPath(".")
v.AddConfigPath("/etc/dishook/")
v.ReadInConfig()
v.SetEnvPrefix(appName)
v.AutomaticEnv()
return v
}