mirror of
https://github.com/spf13/viper
synced 2025-05-11 22:57:21 +00:00
Fix ConfigFileUsed to return config in search path
Returns the config file found in search paths as well instead of just the configFile property.
This commit is contained in:
parent
c42a305a4b
commit
cabc2530d4
2 changed files with 24 additions and 2 deletions
7
viper.go
7
viper.go
|
@ -459,8 +459,11 @@ func (v *Viper) getEnv(key string) (string, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConfigFileUsed returns the file used to populate the config registry.
|
// ConfigFileUsed returns the file used to populate the config registry.
|
||||||
func ConfigFileUsed() string { return v.ConfigFileUsed() }
|
func ConfigFileUsed() string { return v.ConfigFileUsed() }
|
||||||
func (v *Viper) ConfigFileUsed() string { return v.configFile }
|
func (v *Viper) ConfigFileUsed() string {
|
||||||
|
f, _ := v.getConfigFile()
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
// AddConfigPath adds a path for Viper to search for the config file in.
|
// AddConfigPath adds a path for Viper to search for the config file in.
|
||||||
// Can be called multiple times to define multiple search paths.
|
// Can be called multiple times to define multiple search paths.
|
||||||
|
|
|
@ -300,6 +300,25 @@ func (s *stringValue) String() string {
|
||||||
return string(*s)
|
return string(*s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigFileUsed(t *testing.T) {
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
|
err := fs.Mkdir("/tmp/config", 0777)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = fs.Create("/tmp/config/myconfig.yaml")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
v := New()
|
||||||
|
|
||||||
|
v.SetFs(fs)
|
||||||
|
v.SetConfigName("myconfig")
|
||||||
|
v.AddConfigPath("/tmp/config")
|
||||||
|
|
||||||
|
filename := v.ConfigFileUsed()
|
||||||
|
assert.Equal(t, "/tmp/config/myconfig.yaml", filename)
|
||||||
|
}
|
||||||
|
|
||||||
func TestBasics(t *testing.T) {
|
func TestBasics(t *testing.T) {
|
||||||
SetConfigFile("/tmp/config.yaml")
|
SetConfigFile("/tmp/config.yaml")
|
||||||
filename, err := v.getConfigFile()
|
filename, err := v.getConfigFile()
|
||||||
|
|
Loading…
Add table
Reference in a new issue