find config file which with extension first.

This commit is contained in:
tick 2020-02-18 21:18:05 +08:00
parent 97ee7adfef
commit 13632a2ce0
2 changed files with 32 additions and 6 deletions

View File

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

View File

@ -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"))