add ChainMergeConfigFiles

This commit is contained in:
satotake 2017-04-03 02:11:29 +09:00
parent 84f94806c6
commit 1b560de7b7

View file

@ -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{})