mirror of
https://github.com/spf13/viper
synced 2025-05-10 22:27:18 +00:00
Support config files with no extensions
This commit is contained in:
parent
3349bd9cc2
commit
1655def7e9
2 changed files with 21 additions and 0 deletions
4
viper.go
4
viper.go
|
@ -1863,6 +1863,10 @@ func (v *Viper) getConfigFile() (string, error) {
|
||||||
|
|
||||||
func (v *Viper) searchInPath(in string) (filename string) {
|
func (v *Viper) searchInPath(in string) (filename string) {
|
||||||
jww.DEBUG.Println("Searching for config in ", in)
|
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 {
|
for _, ext := range SupportedExts {
|
||||||
jww.DEBUG.Println("Checking for", filepath.Join(in, v.configName+"."+ext))
|
jww.DEBUG.Println("Checking for", filepath.Join(in, v.configName+"."+ext))
|
||||||
if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {
|
if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -273,6 +274,22 @@ func TestBasics(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
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) {
|
func TestDefault(t *testing.T) {
|
||||||
SetDefault("age", 45)
|
SetDefault("age", 45)
|
||||||
assert.Equal(t, 45, Get("age"))
|
assert.Equal(t, 45, Get("age"))
|
||||||
|
|
Loading…
Add table
Reference in a new issue