From 83ab21f53496b6bab591cc480225c4c3d10fe28d Mon Sep 17 00:00:00 2001 From: Reza Mohammadi Date: Mon, 21 Nov 2016 15:46:56 +0330 Subject: [PATCH] Support multiple config change listeners --- viper.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/viper.go b/viper.go index 4ed2d40..2eba734 100644 --- a/viper.go +++ b/viper.go @@ -156,7 +156,7 @@ type Viper struct { aliases map[string]string typeByDefValue bool - onConfigChange func(fsnotify.Event) + onConfigChangeList []func(fsnotify.Event) } // 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. 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) } + +// OnConfigChange adds a change listener to the list of current listeners 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() } @@ -262,7 +268,9 @@ func (v *Viper) WatchConfig() { if err != nil { log.Println("error:", err) } - v.onConfigChange(event) + for _, onConfigChange := range v.onConfigChangeList { + onConfigChange(event) + } } } case err := <-watcher.Errors: