Skip to content

Commit

Permalink
Support config files with no extensions (#1)
Browse files Browse the repository at this point in the history
Support config files with no extensions
  • Loading branch information
pedromss authored Jun 20, 2019
2 parents 3349bd9 + 1655def commit 17f7fc1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"runtime"
"sort"
Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit 17f7fc1

Please sign in to comment.