-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
88 lines (71 loc) · 2.21 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package config
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/rs/zerolog/log"
"time"
"github.com/spf13/viper"
)
const AppName = "nixeepass"
var onConfigChange []func(fsnotify.Event)
// 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
Unmarshal(rawVal interface{}, opts ...viper.DecoderConfigOption) error
UnmarshalKey(key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error
}
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(AppName)
}
func readViperConfig(appName string) *viper.Viper {
v := viper.New()
v.SetEnvPrefix(appName)
v.AutomaticEnv()
v.SetConfigName(fmt.Sprintf("%s-config", appName))
// docker mount points
v.AddConfigPath(fmt.Sprintf("/etc/%s/", appName))
v.AddConfigPath(fmt.Sprintf("$HOME/.%s", appName))
// for development
v.AddConfigPath("config")
err := v.ReadInConfig()
if err != nil { // Handle errors reading the config file
log.Panic().Err(err).Msg("fatal error config file")
}
v.OnConfigChange(OnConfigChange)
v.WatchConfig()
return v
}
func OnConfigChange(e fsnotify.Event) {
log.Info().Str("filename", e.Name).Int("len(onConfigChange)", len(onConfigChange)).Msg("Config file changed")
for _, f := range onConfigChange {
f(e)
}
}
func RegisterOnConfigChange(run func(in fsnotify.Event)) {
onConfigChange = append(onConfigChange, run)
}