mirror of
https://github.com/spf13/viper
synced 2024-12-23 12:07:02 +00:00
find config file which with extension first.
This commit is contained in:
parent
97ee7adfef
commit
13632a2ce0
2 changed files with 32 additions and 6 deletions
14
viper.go
14
viper.go
|
@ -1978,12 +1978,6 @@ func (v *Viper) searchInPath(in string) (filename string) {
|
|||
}
|
||||
}
|
||||
|
||||
if v.configType != "" {
|
||||
if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b {
|
||||
return filepath.Join(in, v.configName)
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
|
@ -1998,6 +1992,14 @@ func (v *Viper) findConfigFile() (string, error) {
|
|||
return file, nil
|
||||
}
|
||||
}
|
||||
|
||||
for _, in := range v.configPaths {
|
||||
if v.configType != "" {
|
||||
if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b {
|
||||
return filepath.Join(in, v.configName), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
|
||||
}
|
||||
|
||||
|
|
|
@ -361,6 +361,30 @@ func TestSearchInPath_FilesOnly(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestSearchInPath_ExtensionFirst(t *testing.T) {
|
||||
exceptFile := "/tmp/withExtension.yaml"
|
||||
_, err := v.fs.Create(exceptFile)
|
||||
defer func() {
|
||||
_ = v.fs.Remove("/tmp/withExtension.yaml")
|
||||
}()
|
||||
|
||||
assert.NoError(t, err)
|
||||
_, err = v.fs.Create("./.withoutExtension")
|
||||
assert.NoError(t, err)
|
||||
defer func() {
|
||||
_ = v.fs.Remove("./.withoutExtension")
|
||||
}()
|
||||
|
||||
SetConfigName("withExtension")
|
||||
SetConfigType("yaml")
|
||||
AddConfigPath(".")
|
||||
AddConfigPath("/tmp")
|
||||
|
||||
filename, err := v.getConfigFile()
|
||||
assert.Equal(t, exceptFile, filename)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDefault(t *testing.T) {
|
||||
SetDefault("age", 45)
|
||||
assert.Equal(t, 45, Get("age"))
|
||||
|
|
Loading…
Reference in a new issue