mirror of
https://github.com/spf13/viper
synced 2024-12-23 12:07:02 +00:00
rename configFiles > configFileChain
This commit is contained in:
parent
33923af746
commit
fb72938b55
2 changed files with 25 additions and 25 deletions
32
viper.go
32
viper.go
|
@ -147,7 +147,7 @@ type Viper struct {
|
||||||
// Name of file to look for inside the path
|
// Name of file to look for inside the path
|
||||||
configName string
|
configName string
|
||||||
configFile string
|
configFile string
|
||||||
configFiles []string
|
configFileChain []string
|
||||||
configType string
|
configType string
|
||||||
envPrefix string
|
envPrefix string
|
||||||
|
|
||||||
|
@ -292,12 +292,12 @@ func (v *Viper) SetConfigFile(in string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetConfigFiles explicitly defines slice of the path, name and extension of the config files
|
// SetConfigFileChain 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
|
// Viper will use these and not check any of the config paths
|
||||||
func SetConfigFiles(in []string) { v.SetConfigFiles(in) }
|
func SetConfigChain(in []string) { v.SetConfigFileChain(in) }
|
||||||
func (v *Viper) SetConfigFiles(in []string) {
|
func (v *Viper) SetConfigFileChain(in []string) {
|
||||||
if len(in) > 0 {
|
if len(in) > 0 {
|
||||||
v.configFiles = in
|
v.configFileChain = in
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1141,17 +1141,17 @@ func (v *Viper) MergeInConfig() error {
|
||||||
return v.MergeConfig(bytes.NewReader(file))
|
return v.MergeConfig(bytes.NewReader(file))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChainMergeConfigfiles repeatedly merge with configFiles
|
// MergeConfigFileChain repeatedly merge with configFileChain
|
||||||
func ChainMergeConfigFiles() error { return v.ChainMergeConfigFiles() }
|
func MergeConfigFileChain() error { return v.MergeConfigFileChain() }
|
||||||
func (v *Viper) ChainMergeConfigFiles() error {
|
func (v *Viper) MergeConfigFileChain() error {
|
||||||
jww.INFO.Println("Attempting to chain merge with configFiles")
|
jww.INFO.Println("Attempting to chain merge with configFileChain")
|
||||||
|
|
||||||
configFiles, err := v.getConfigFiles()
|
configFileChain, err := v.getConfigFileChain()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, filename := range configFiles {
|
for i, filename := range configFileChain {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
v.SetConfigFile(filename)
|
v.SetConfigFile(filename)
|
||||||
err := v.ReadInConfig()
|
err := v.ReadInConfig()
|
||||||
|
@ -1544,11 +1544,11 @@ func (v *Viper) getConfigFile() (string, error) {
|
||||||
return v.getConfigFile()
|
return v.getConfigFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) getConfigFiles() ([]string, error) {
|
func (v *Viper) getConfigFileChain() ([]string, error) {
|
||||||
// if explicitly set, then use it
|
// if explicitly set, then use it
|
||||||
// else search config file and return as slice
|
// else search config file and return as slice
|
||||||
if len(v.configFiles) > 0 {
|
if len(v.configFileChain) > 0 {
|
||||||
return v.configFiles, nil
|
return v.configFileChain, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cf, err := v.findConfigFile()
|
cf, err := v.findConfigFile()
|
||||||
|
@ -1556,8 +1556,8 @@ func (v *Viper) getConfigFiles() ([]string, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
v.configFiles = []string{cf}
|
v.configFileChain = []string{cf}
|
||||||
return v.getConfigFiles()
|
return v.getConfigFileChain()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) searchInPath(in string) (filename string) {
|
func (v *Viper) searchInPath(in string) (filename string) {
|
||||||
|
|
|
@ -1193,7 +1193,7 @@ func initConfigsForMerge(t *testing.T) (string, string, func()) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChainMergeConfig(t *testing.T) {
|
func TestMergeConfigChain(t *testing.T) {
|
||||||
|
|
||||||
root, config, cleanup := initConfigsForMerge(t)
|
root, config, cleanup := initConfigsForMerge(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
@ -1209,8 +1209,8 @@ func TestChainMergeConfig(t *testing.T) {
|
||||||
configfile := path.Join(root, e.Name(), config)
|
configfile := path.Join(root, e.Name(), config)
|
||||||
configfiles = append(configfiles, configfile)
|
configfiles = append(configfiles, configfile)
|
||||||
|
|
||||||
v.SetConfigFiles(configfiles[:i+1])
|
v.SetConfigFileChain(configfiles[:i+1])
|
||||||
err = v.ChainMergeConfigFiles()
|
err = v.MergeConfigFileChain()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, `value is `+e.Name(), v.GetString(`key`))
|
assert.Equal(t, `value is `+e.Name(), v.GetString(`key`))
|
||||||
assert.Equal(t, `a`, v.GetString(`a`))
|
assert.Equal(t, `a`, v.GetString(`a`))
|
||||||
|
@ -1230,7 +1230,7 @@ func TestChainMergeConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotSetConfigFilesChainMerge(t *testing.T) {
|
func TestNotSetConfigFileChainMerge(t *testing.T) {
|
||||||
// If not set, behavior is identical to ReadInConfig
|
// If not set, behavior is identical to ReadInConfig
|
||||||
|
|
||||||
root, config, cleanup := initDirs(t)
|
root, config, cleanup := initDirs(t)
|
||||||
|
@ -1248,7 +1248,7 @@ func TestNotSetConfigFilesChainMerge(t *testing.T) {
|
||||||
}
|
}
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
err = v.ChainMergeConfigFiles()
|
err = v.MergeConfigFileChain()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
assert.Equal(t, `value is `+path.Base(v.configPaths[0]), v.GetString(`key`))
|
assert.Equal(t, `value is `+path.Base(v.configPaths[0]), v.GetString(`key`))
|
||||||
|
|
Loading…
Reference in a new issue