ReadBufConfig

This commit is contained in:
oliveagle 2015-05-08 17:13:33 +08:00 committed by spf13
parent be782f3fee
commit 3492885e84
2 changed files with 89 additions and 0 deletions

View file

@ -36,6 +36,8 @@ import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/pflag" "github.com/spf13/pflag"
crypt "github.com/xordataexchange/crypt/config" crypt "github.com/xordataexchange/crypt/config"
// log "github.com/oliveagle/seelog"
) )
var v *Viper var v *Viper
@ -268,6 +270,8 @@ func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error {
v.remoteProviders = append(v.remoteProviders, rp) v.remoteProviders = append(v.remoteProviders, rp)
} }
} }
// log.Info("remoteProviders", v.remoteProviders)
return nil return nil
} }
@ -716,6 +720,13 @@ func (v *Viper) ReadInConfig() error {
return nil return nil
} }
func ReadBufConfig(buf *bytes.Buffer) error { return v.ReadBufConfig(buf) }
func (v *Viper) ReadBufConfig(buf *bytes.Buffer) error {
v.config = make(map[string]interface{})
v.marshalReader(buf, v.config)
return nil
}
// Attempts to get configuration from a remote source // Attempts to get configuration from a remote source
// and read it in the remote configuration registry. // and read it in the remote configuration registry.
func ReadRemoteConfig() error { return v.ReadRemoteConfig() } func ReadRemoteConfig() error { return v.ReadRemoteConfig() }
@ -727,6 +738,15 @@ func (v *Viper) ReadRemoteConfig() error {
return nil return nil
} }
func WatchRemoteConfig() error { return v.WatchRemoteConfig() }
func (v *Viper) WatchRemoteConfig() error {
err := v.watchKeyValueConfig()
if err != nil {
return err
}
return nil
}
// Marshall a Reader into a map // Marshall a Reader into a map
// Should probably be an unexported function // Should probably be an unexported function
func marshalReader(in io.Reader, c map[string]interface{}) { v.marshalReader(in, c) } func marshalReader(in io.Reader, c map[string]interface{}) { v.marshalReader(in, c) }
@ -788,6 +808,61 @@ func (v *Viper) getRemoteConfig(provider *remoteProvider) (map[string]interface{
return v.kvstore, err return v.kvstore, err
} }
// retrieve the first found remote configuration
func (v *Viper) watchKeyValueConfig() error {
// log.Info("ahahahah==========================================")
// log.Info("remoteProviders", v.remoteProviders)
for _, rp := range v.remoteProviders {
val, err := v.watchRemoteConfig(rp)
if err != nil {
continue
}
v.kvstore = val
return nil
}
return RemoteConfigError("No Files Found")
}
func (v *Viper) watchRemoteConfig(provider *remoteProvider) (map[string]interface{}, error) {
// defer log.Flush()
// log.Info("ahahahah==========================================")
var cm crypt.ConfigManager
var err error
if provider.secretKeyring != "" {
kr, err := os.Open(provider.secretKeyring)
defer kr.Close()
if err != nil {
return nil, err
}
if provider.provider == "etcd" {
cm, err = crypt.NewEtcdConfigManager([]string{provider.endpoint}, kr)
} else {
cm, err = crypt.NewConsulConfigManager([]string{provider.endpoint}, kr)
}
} else {
if provider.provider == "etcd" {
cm, err = crypt.NewStandardEtcdConfigManager([]string{provider.endpoint})
} else {
cm, err = crypt.NewStandardConsulConfigManager([]string{provider.endpoint})
}
}
if err != nil {
return nil, err
}
resp := <-cm.Watch(provider.path, nil)
// b, err := cm.Watch(provider.path, nil)
err = resp.Error
if err != nil {
return nil, err
}
reader := bytes.NewReader(resp.Value)
v.marshalReader(reader, v.kvstore)
return v.kvstore, err
}
// Return all keys regardless where they are set // Return all keys regardless where they are set
func AllKeys() []string { return v.AllKeys() } func AllKeys() []string { return v.AllKeys() }
func (v *Viper) AllKeys() []string { func (v *Viper) AllKeys() []string {

View file

@ -537,3 +537,17 @@ func TestFindsNestedKeys(t *testing.T) {
} }
} }
func TestReadBufConfig(t *testing.T) {
v := New()
v.SetConfigType("yaml")
v.ReadBufConfig(bytes.NewBuffer(yamlExample))
t.Log(v.AllKeys())
assert.True(t, v.InConfig("name"))
assert.False(t, v.InConfig("state"))
assert.Equal(t, "steve", v.Get("name"))
assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, v.Get("hobbies"))
assert.Equal(t, map[interface{}]interface{}{"jacket": "leather", "trousers": "denim"}, v.Get("clothing"))
assert.Equal(t, 35, v.Get("age"))
}