From 1b560de7b7aa35596ac371460e52b5e8dfc043c6 Mon Sep 17 00:00:00 2001 From: satotake Date: Mon, 3 Apr 2017 02:11:29 +0900 Subject: [PATCH] add ChainMergeConfigFiles --- viper.go | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/viper.go b/viper.go index 22a2ed8..96b16f7 100644 --- a/viper.go +++ b/viper.go @@ -52,7 +52,7 @@ func init() { type remoteConfigFactory interface { Get(rp RemoteProvider) (io.Reader, error) Watch(rp RemoteProvider) (io.Reader, error) - WatchChannel(rp RemoteProvider)(<-chan *RemoteResponse, chan bool) + WatchChannel(rp RemoteProvider) (<-chan *RemoteResponse, chan bool) } // RemoteConfig is optional, see the remote package @@ -145,10 +145,11 @@ type Viper struct { remoteProviders []*defaultRemoteProvider // Name of file to look for inside the path - configName string - configFile string - configType string - envPrefix string + configName string + configFile string + configFiles []string + configType string + envPrefix string automaticEnvApplied bool envKeyReplacer *strings.Replacer @@ -291,6 +292,15 @@ func (v *Viper) SetConfigFile(in string) { } } +// SetConfigFiles explicitly defines slice of the path, name and extension of the config files +// Viper will use these and not check any of the config paths +func SetConfigFiles(in []string) { v.SetConfigFiles(in) } +func (v *Viper) SetConfigFiles(in []string) { + if len(in) > 0 { + v.configFiles = in + } +} + // SetEnvPrefix defines a prefix that ENVIRONMENT variables will use. // E.g. if your prefix is "spf", the env registry // will look for env. variables that start with "SPF_" @@ -1131,8 +1141,29 @@ func (v *Viper) MergeInConfig() error { return v.MergeConfig(bytes.NewReader(file)) } -// ReadConfig will read a configuration file, setting existing keys to nil if the -// key does not exist in the file. +// ChainMergeConfigfiles repeatedly merge with configFiles +func ChainMergeConfigFiles() error { return v.ChainMergeConfigFiles() } +func (v *Viper) ChainMergeConfigFiles() error { + jww.INFO.Println("Attempting to chain merge with configFiles") + + for i, filename := range v.configFiles { + if i == 0 { + v.SetConfigFile(filename) + err := v.ReadInConfig() + if err != nil { + return err + } + } else { + v.SetConfigFile(filename) + err := v.MergeInConfig() + if err != nil { + return err + } + } + } + return nil +} + func ReadConfig(in io.Reader) error { return v.ReadConfig(in) } func (v *Viper) ReadConfig(in io.Reader) error { v.config = make(map[string]interface{})