From b42d870a30c96cefadcd548e5c55fde8f718bba1 Mon Sep 17 00:00:00 2001 From: lysu Date: Wed, 9 Mar 2016 00:50:34 +0800 Subject: [PATCH] enhance(remote): one RemoteProvider support multiple addresses - split provider url by commas - update `AddRemoteProvider` comment let us can config multiple addresses in one provider. --- remote/remote.go | 14 ++++++++++---- viper.go | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/remote/remote.go b/remote/remote.go index faaf3b3..1453134 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -12,6 +12,7 @@ import ( crypt "github.com/xordataexchange/crypt/config" "io" "os" + "strings" ) type remoteConfigProvider struct{} @@ -54,15 +55,15 @@ func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) { return nil, err } if rp.Provider() == "etcd" { - cm, err = crypt.NewEtcdConfigManager([]string{rp.Endpoint()}, kr) + cm, err = crypt.NewEtcdConfigManager(toMachines(rp.Endpoint()), kr) } else { - cm, err = crypt.NewConsulConfigManager([]string{rp.Endpoint()}, kr) + cm, err = crypt.NewConsulConfigManager(toMachines(rp.Endpoint()), kr) } } else { if rp.Provider() == "etcd" { - cm, err = crypt.NewStandardEtcdConfigManager([]string{rp.Endpoint()}) + cm, err = crypt.NewStandardEtcdConfigManager(toMachines(rp.Endpoint())) } else { - cm, err = crypt.NewStandardConsulConfigManager([]string{rp.Endpoint()}) + cm, err = crypt.NewStandardConsulConfigManager(toMachines(rp.Endpoint())) } } if err != nil { @@ -72,6 +73,11 @@ func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) { } +func toMachines(endpoint string) []string { + machines := strings.Split(endpoint, ",") + return machines +} + func init() { viper.RemoteConfig = &remoteConfigProvider{} } diff --git a/viper.go b/viper.go index 7a49a0a..8efa232 100644 --- a/viper.go +++ b/viper.go @@ -326,9 +326,10 @@ func (v *Viper) AddConfigPath(in string) { } // AddRemoteProvider adds a remote configuration source. -// Remote Providers are searched in the order they are added. +// Remote Providers are searched in the order they are added, and use first found provider. // provider is a string value, "etcd" or "consul" are currently supported. // endpoint is the url. etcd requires http://ip:port consul requires ip:port +// multiple addresses can configured in one url separated by commas // path is the path in the k/v store to retrieve configuration // To retrieve a config file called myapp.json from /configs/myapp.json // you should set path to /configs and set config name (SetConfigName()) to @@ -1156,36 +1157,36 @@ func AllKeys() []string { return v.AllKeys() } func (v *Viper) AllKeys() []string { m := map[string]struct{}{} - for key, _ := range v.defaults { + for key := range v.defaults { m[strings.ToLower(key)] = struct{}{} } - for key, _ := range v.pflags { + for key := range v.pflags { m[strings.ToLower(key)] = struct{}{} } - for key, _ := range v.env { + for key := range v.env { m[strings.ToLower(key)] = struct{}{} } - for key, _ := range v.config { + for key := range v.config { m[strings.ToLower(key)] = struct{}{} } - for key, _ := range v.kvstore { + for key := range v.kvstore { m[strings.ToLower(key)] = struct{}{} } - for key, _ := range v.override { + for key := range v.override { m[strings.ToLower(key)] = struct{}{} } - for key, _ := range v.aliases { + for key := range v.aliases { m[strings.ToLower(key)] = struct{}{} } a := []string{} - for x, _ := range m { + for x := range m { a = append(a, x) }