From 18f781b94ae0289653c5207aaae85ca36733c064 Mon Sep 17 00:00:00 2001 From: RavenZZ Date: Thu, 8 Feb 2018 16:54:56 +0800 Subject: [PATCH 1/3] WatchRemoteConfigOnChannel --- viper.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/viper.go b/viper.go index 64f006a..0c1be50 100644 --- a/viper.go +++ b/viper.go @@ -162,7 +162,8 @@ type Viper struct { aliases map[string]string typeByDefValue bool - onConfigChange func(fsnotify.Event) + onConfigChange func(fsnotify.Event) + onRemoteConfigChange func() } // New returns an initialized Viper instance. @@ -237,6 +238,11 @@ func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) { v.onConfigChange = run } +func OnRemoteConfigChange(run func()) { v.OnRemoteConfigChange(run) } +func (v *Viper) OnRemoteConfigChange(run func()) { + v.onRemoteConfigChange = run +} + func WatchConfig() { v.WatchConfig() } func (v *Viper) WatchConfig() { go func() { @@ -1286,6 +1292,7 @@ func (v *Viper) WatchRemoteConfig() error { return v.watchKeyValueConfig() } +func WatchRemoteConfigOnChannel() error { return v.watchKeyValueConfigOnChannel() } func (v *Viper) WatchRemoteConfigOnChannel() error { return v.watchKeyValueConfigOnChannel() } @@ -1338,13 +1345,14 @@ func (v *Viper) watchKeyValueConfigOnChannel() error { for _, rp := range v.remoteProviders { respc, _ := RemoteConfig.WatchChannel(rp) //Todo: Add quit channel - go func(rc <-chan *RemoteResponse) { - for { - b := <-rc - reader := bytes.NewReader(b.Value) - v.unmarshalReader(reader, v.kvstore) - } - }(respc) + + b := <-respc + reader := bytes.NewReader(b.Value) + v.unmarshalReader(reader, v.kvstore) + + if v.onRemoteConfigChange != nil { + v.onRemoteConfigChange() + } return nil } return RemoteConfigError("No Files Found") From 047b2100ff8094e4f895a4b7e935121e2d3d9bb5 Mon Sep 17 00:00:00 2001 From: RavenZZ Date: Fri, 9 Feb 2018 10:21:38 +0800 Subject: [PATCH 2/3] fix go imports sort --- remote/remote.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/remote/remote.go b/remote/remote.go index 68a35d6..810d070 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -8,10 +8,11 @@ package remote import ( "bytes" - "github.com/spf13/viper" - crypt "github.com/xordataexchange/crypt/config" "io" "os" + + "github.com/spf13/viper" + crypt "github.com/xordataexchange/crypt/config" ) type remoteConfigProvider struct{} From d18964fe224dca885fab28431b0ef32d626daf9f Mon Sep 17 00:00:00 2001 From: RavenZZ Date: Fri, 9 Feb 2018 10:31:29 +0800 Subject: [PATCH 3/3] gofmt -d . --- viper.go | 1 - 1 file changed, 1 deletion(-) diff --git a/viper.go b/viper.go index 18a59b8..edb4446 100644 --- a/viper.go +++ b/viper.go @@ -179,7 +179,6 @@ type Viper struct { aliases map[string]string typeByDefValue bool - onConfigChange func(fsnotify.Event) onRemoteConfigChange func()