Fix config watch

* Only add *the* config file, not all possible folders
* Trigger reload on both write and create events;
  the latter is what we get from atomic save editors (like TextMate) once https://github.com/go-fsnotify/fsnotify/pull/111 is merged

See #142
This commit is contained in:
Bjørn Erik Pedersen 2016-01-11 13:34:45 +01:00
parent 0c82789feb
commit cc70319ebc

View file

@ -242,7 +242,7 @@ func (v *Viper) WatchConfig() {
for { for {
select { select {
case event := <-watcher.Events: case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write { if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
err := v.ReadInConfig() err := v.ReadInConfig()
if err != nil { if err != nil {
log.Println("error:", err) log.Println("error:", err)
@ -255,16 +255,7 @@ func (v *Viper) WatchConfig() {
} }
}() }()
if v.configFile != "" { watcher.Add(v.getConfigFile())
watcher.Add(v.configFile)
} else {
for _, x := range v.configPaths {
err = watcher.Add(x)
if err != nil {
log.Fatal(err)
}
}
}
<-done <-done
}() }()
} }