-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviperfile.go
35 lines (29 loc) · 932 Bytes
/
viperfile.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
package viperfile
import (
"log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
// ReadLocal reads a local config file from the top-level directory
// fileName should be without the extension
// binding is a pointer to the struct that the config will be bound
func ReadLocal(fileName string, binding interface{}) {
viper.AddConfigPath(".")
viper.SetConfigName(fileName)
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Cannot find the config file! It should be included in your top-level directory.")
}
if viper.Unmarshal(binding) != nil {
log.Fatalf("Unable to read the config file!")
}
log.Printf("Using config %+v\n", binding)
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
if viper.Unmarshal(binding) != nil {
log.Println("Failed to read the new config file! Using the old config from memory.")
} else {
log.Printf("Updated config %+v\n", binding)
}
})
}