mirror of
https://github.com/spf13/viper
synced 2025-05-06 12:17:18 +00:00
Support multiple config change listeners
This commit is contained in:
parent
651d9d916a
commit
83ab21f534
1 changed files with 11 additions and 3 deletions
14
viper.go
14
viper.go
|
@ -156,7 +156,7 @@ type Viper struct {
|
||||||
aliases map[string]string
|
aliases map[string]string
|
||||||
typeByDefValue bool
|
typeByDefValue bool
|
||||||
|
|
||||||
onConfigChange func(fsnotify.Event)
|
onConfigChangeList []func(fsnotify.Event)
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns an initialized Viper instance.
|
// New returns an initialized Viper instance.
|
||||||
|
@ -226,9 +226,15 @@ var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props
|
||||||
// SupportedRemoteProviders are universally supported remote providers.
|
// SupportedRemoteProviders are universally supported remote providers.
|
||||||
var SupportedRemoteProviders = []string{"etcd", "consul"}
|
var SupportedRemoteProviders = []string{"etcd", "consul"}
|
||||||
|
|
||||||
|
// OnConfigChange adds a change listener to the list of current listeners
|
||||||
func OnConfigChange(run func(in fsnotify.Event)) { v.OnConfigChange(run) }
|
func OnConfigChange(run func(in fsnotify.Event)) { v.OnConfigChange(run) }
|
||||||
|
|
||||||
|
// OnConfigChange adds a change listener to the list of current listeners
|
||||||
func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) {
|
func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) {
|
||||||
v.onConfigChange = run
|
if v.onConfigChangeList == nil {
|
||||||
|
v.onConfigChangeList = make([]func(fsnotify.Event), 0, 1)
|
||||||
|
}
|
||||||
|
v.onConfigChangeList = append(v.onConfigChangeList, run)
|
||||||
}
|
}
|
||||||
|
|
||||||
func WatchConfig() { v.WatchConfig() }
|
func WatchConfig() { v.WatchConfig() }
|
||||||
|
@ -262,7 +268,9 @@ func (v *Viper) WatchConfig() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error:", err)
|
log.Println("error:", err)
|
||||||
}
|
}
|
||||||
v.onConfigChange(event)
|
for _, onConfigChange := range v.onConfigChangeList {
|
||||||
|
onConfigChange(event)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case err := <-watcher.Errors:
|
case err := <-watcher.Errors:
|
||||||
|
|
Loading…
Add table
Reference in a new issue