Allow exists util function to take afero.Fs so it can be used with non-deafult instances of Viper (#405)

Signed-off-by: Jeff Lindsay <progrium@gmail.com>
This commit is contained in:
Jeff Lindsay 2017-11-09 14:57:16 -06:00 committed by Brian Ketelsen
parent 8ef37cbca7
commit 4dddf7c62e
2 changed files with 4 additions and 3 deletions

View file

@ -24,6 +24,7 @@ import (
"github.com/hashicorp/hcl" "github.com/hashicorp/hcl"
"github.com/magiconair/properties" "github.com/magiconair/properties"
toml "github.com/pelletier/go-toml" toml "github.com/pelletier/go-toml"
"github.com/spf13/afero"
"github.com/spf13/cast" "github.com/spf13/cast"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -121,8 +122,8 @@ func absPathify(inPath string) string {
} }
// Check if File / Directory Exists // Check if File / Directory Exists
func exists(path string) (bool, error) { func exists(fs afero.Fs, path string) (bool, error) {
_, err := v.fs.Stat(path) _, err := fs.Stat(path)
if err == nil { if err == nil {
return true, nil return true, nil
} }

View file

@ -1537,7 +1537,7 @@ func (v *Viper) searchInPath(in string) (filename string) {
jww.DEBUG.Println("Searching for config in ", in) jww.DEBUG.Println("Searching for config in ", in)
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(filepath.Join(in, v.configName+"."+ext)); b { if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {
jww.DEBUG.Println("Found: ", filepath.Join(in, v.configName+"."+ext)) jww.DEBUG.Println("Found: ", filepath.Join(in, v.configName+"."+ext))
return filepath.Join(in, v.configName+"."+ext) return filepath.Join(in, v.configName+"."+ext)
} }