419 var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"} |
419 var SupportedExts = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"} |
420 |
420 |
421 // SupportedRemoteProviders are universally supported remote providers. |
421 // SupportedRemoteProviders are universally supported remote providers. |
422 var SupportedRemoteProviders = []string{"etcd", "etcd3", "consul", "firestore"} |
422 var SupportedRemoteProviders = []string{"etcd", "etcd3", "consul", "firestore"} |
423 |
423 |
|
424 // OnConfigChange sets the event handler that is called when a config file changes. |
424 func OnConfigChange(run func(in fsnotify.Event)) { v.OnConfigChange(run) } |
425 func OnConfigChange(run func(in fsnotify.Event)) { v.OnConfigChange(run) } |
|
426 |
|
427 // OnConfigChange sets the event handler that is called when a config file changes. |
425 func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) { |
428 func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) { |
426 v.onConfigChange = run |
429 v.onConfigChange = run |
427 } |
430 } |
428 |
431 |
|
432 // WatchConfig starts watching a config file for changes. |
429 func WatchConfig() { v.WatchConfig() } |
433 func WatchConfig() { v.WatchConfig() } |
430 |
434 |
|
435 // WatchConfig starts watching a config file for changes. |
431 func (v *Viper) WatchConfig() { |
436 func (v *Viper) WatchConfig() { |
432 initWG := sync.WaitGroup{} |
437 initWG := sync.WaitGroup{} |
433 initWG.Add(1) |
438 initWG.Add(1) |
434 go func() { |
439 go func() { |
435 watcher, err := newWatcher() |
440 watcher, err := newWatcher() |
461 } |
466 } |
462 currentConfigFile, _ := filepath.EvalSymlinks(filename) |
467 currentConfigFile, _ := filepath.EvalSymlinks(filename) |
463 // we only care about the config file with the following cases: |
468 // we only care about the config file with the following cases: |
464 // 1 - if the config file was modified or created |
469 // 1 - if the config file was modified or created |
465 // 2 - if the real path to the config file changed (eg: k8s ConfigMap replacement) |
470 // 2 - if the real path to the config file changed (eg: k8s ConfigMap replacement) |
466 const writeOrCreateMask = fsnotify.Write | fsnotify.Create |
|
467 if (filepath.Clean(event.Name) == configFile && |
471 if (filepath.Clean(event.Name) == configFile && |
468 event.Op&writeOrCreateMask != 0) || |
472 (event.Has(fsnotify.Write) || event.Has(fsnotify.Create))) || |
469 (currentConfigFile != "" && currentConfigFile != realConfigFile) { |
473 (currentConfigFile != "" && currentConfigFile != realConfigFile) { |
470 realConfigFile = currentConfigFile |
474 realConfigFile = currentConfigFile |
471 err := v.ReadInConfig() |
475 err := v.ReadInConfig() |
472 if err != nil { |
476 if err != nil { |
473 log.Printf("error reading config file: %v\n", err) |
477 log.Printf("error reading config file: %v\n", err) |
474 } |
478 } |
475 if v.onConfigChange != nil { |
479 if v.onConfigChange != nil { |
476 v.onConfigChange(event) |
480 v.onConfigChange(event) |
477 } |
481 } |
478 } else if filepath.Clean(event.Name) == configFile && |
482 } else if filepath.Clean(event.Name) == configFile && event.Has(fsnotify.Remove) { |
479 event.Op&fsnotify.Remove != 0 { |
|
480 eventsWG.Done() |
483 eventsWG.Done() |
481 return |
484 return |
482 } |
485 } |
483 |
486 |
484 case err, ok := <-watcher.Errors: |
487 case err, ok := <-watcher.Errors: |