mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
apply review comments
use masks for checking the events. Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
This commit is contained in:
parent
242f4890f5
commit
c1250e5dd7
2 changed files with 6 additions and 3 deletions
7
viper.go
7
viper.go
|
@ -260,6 +260,7 @@ func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func WatchConfig() { v.WatchConfig() }
|
func WatchConfig() { v.WatchConfig() }
|
||||||
|
|
||||||
func (v *Viper) WatchConfig() {
|
func (v *Viper) WatchConfig() {
|
||||||
initWG := sync.WaitGroup{}
|
initWG := sync.WaitGroup{}
|
||||||
initWG.Add(1)
|
initWG.Add(1)
|
||||||
|
@ -294,8 +295,9 @@ func (v *Viper) WatchConfig() {
|
||||||
// we only care about the config file with the following cases:
|
// we only care about the config file with the following cases:
|
||||||
// 1 - if the config file was modified or created
|
// 1 - if the config file was modified or created
|
||||||
// 2 - if the real path to the config file changed (eg: k8s ConfigMap replacement)
|
// 2 - if the real path to the config file changed (eg: k8s ConfigMap replacement)
|
||||||
|
const writeOrCreateMask = fsnotify.Write | fsnotify.Create
|
||||||
if (filepath.Clean(event.Name) == configFile &&
|
if (filepath.Clean(event.Name) == configFile &&
|
||||||
(event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create)) ||
|
event.Op&writeOrCreateMask != 0) ||
|
||||||
(currentConfigFile != "" && currentConfigFile != realConfigFile) {
|
(currentConfigFile != "" && currentConfigFile != realConfigFile) {
|
||||||
realConfigFile = currentConfigFile
|
realConfigFile = currentConfigFile
|
||||||
err := v.ReadInConfig()
|
err := v.ReadInConfig()
|
||||||
|
@ -306,7 +308,7 @@ func (v *Viper) WatchConfig() {
|
||||||
v.onConfigChange(event)
|
v.onConfigChange(event)
|
||||||
}
|
}
|
||||||
} else if filepath.Clean(event.Name) == configFile &&
|
} else if filepath.Clean(event.Name) == configFile &&
|
||||||
event.Op&fsnotify.Remove == fsnotify.Remove {
|
event.Op&fsnotify.Remove&fsnotify.Remove != 0 {
|
||||||
eventsWG.Done()
|
eventsWG.Done()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -324,6 +326,7 @@ func (v *Viper) WatchConfig() {
|
||||||
initWG.Done() // done initalizing the watch in this go routine, so the parent routine can move on...
|
initWG.Done() // done initalizing the watch in this go routine, so the parent routine can move on...
|
||||||
eventsWG.Wait() // now, wait for event loop to end in this go-routine...
|
eventsWG.Wait() // now, wait for event loop to end in this go-routine...
|
||||||
}()
|
}()
|
||||||
|
fmt.Println(" init WG done")
|
||||||
initWG.Wait() // make sure that the go routine above fully ended before returning
|
initWG.Wait() // make sure that the go routine above fully ended before returning
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1426,12 +1426,12 @@ func TestWatchFile(t *testing.T) {
|
||||||
fmt.Printf("test config file: %s\n", configFile)
|
fmt.Printf("test config file: %s\n", configFile)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(1)
|
||||||
v.WatchConfig()
|
v.WatchConfig()
|
||||||
v.OnConfigChange(func(in fsnotify.Event) {
|
v.OnConfigChange(func(in fsnotify.Event) {
|
||||||
t.Logf("config file changed")
|
t.Logf("config file changed")
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
wg.Add(1)
|
|
||||||
// when overwriting the file and waiting for the custom change notification handler to be triggered
|
// when overwriting the file and waiting for the custom change notification handler to be triggered
|
||||||
err := ioutil.WriteFile(configFile, []byte("foo: baz\n"), 0640)
|
err := ioutil.WriteFile(configFile, []byte("foo: baz\n"), 0640)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
Loading…
Reference in a new issue