From 70746da62530b5438cda90d1e9f3c65e75eca75e Mon Sep 17 00:00:00 2001 From: Xuanxin Zhen Date: Sun, 4 Feb 2024 10:55:34 +0800 Subject: [PATCH] feat: support callback when remote config changed --- viper.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/viper.go b/viper.go index 4a9935899..31eddfbc0 100644 --- a/viper.go +++ b/viper.go @@ -261,6 +261,8 @@ type Viper struct { onConfigChange func(fsnotify.Event) logger Logger + + onRemoteConfigChange func() } // New returns an initialized Viper instance. @@ -1864,6 +1866,12 @@ func mergeMaps( } } +func OnRemoteConfigChange(run func()) { v.OnRemoteConfigChange(run) } + +func (v *Viper) OnRemoteConfigChange(run func()) { + v.onRemoteConfigChange = run +} + // ReadRemoteConfig attempts to get configuration from a remote source // and read it in the remote configuration registry. func ReadRemoteConfig() error { return v.ReadRemoteConfig() } @@ -1921,6 +1929,10 @@ func (v *Viper) watchKeyValueConfigOnChannel() error { b := <-rc reader := bytes.NewReader(b.Value) v.unmarshalReader(reader, v.kvstore) + + if v.onRemoteConfigChange != nil { + v.onRemoteConfigChange() + } } }(respc) return nil