Support config files with no extensions

This commit is contained in:
Pedro Silva 2019-06-20 21:32:08 +01:00 committed by Pedro Silva
parent 3349bd9cc2
commit 1655def7e9
2 changed files with 21 additions and 0 deletions

View file

@ -1863,6 +1863,10 @@ func (v *Viper) getConfigFile() (string, error) {
func (v *Viper) searchInPath(in string) (filename string) {
jww.DEBUG.Println("Searching for config in ", in)
if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b {
return filepath.Join(in, v.configName)
}
for _, ext := range SupportedExts {
jww.DEBUG.Println("Checking for", filepath.Join(in, v.configName+"."+ext))
if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {

View file

@ -14,6 +14,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"runtime"
"sort"
@ -273,6 +274,22 @@ func TestBasics(t *testing.T) {
assert.NoError(t, err)
}
func TestSearchInPath(t *testing.T) {
filename := ".dotfilenoext"
path := "/tmp"
file := filepath.Join(path, filename)
SetConfigName(filename)
AddConfigPath(path)
_, createErr := v.fs.Create(file)
defer func() {
_ = v.fs.Remove(file)
}()
assert.NoError(t, createErr)
filename, err := v.getConfigFile()
assert.Equal(t, file, filename)
assert.NoError(t, err)
}
func TestDefault(t *testing.T) {
SetDefault("age", 45)
assert.Equal(t, 45, Get("age"))