From 4cf0bd27897685ffc5c5409b6a2f8ff5b04cf525 Mon Sep 17 00:00:00 2001 From: Matthieu Grieger Date: Fri, 5 Aug 2016 00:24:49 -0700 Subject: [PATCH] Add support for Afero filesystems --- util.go | 2 +- viper.go | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/util.go b/util.go index 3c4a320..6b34e14 100644 --- a/util.go +++ b/util.go @@ -77,7 +77,7 @@ func absPathify(inPath string) string { // Check if File / Directory Exists func exists(path string) (bool, error) { - _, err := os.Stat(path) + _, err := fs.Stat(path) if err == nil { return true, nil } diff --git a/viper.go b/viper.go index d800395..ba3c190 100644 --- a/viper.go +++ b/viper.go @@ -23,7 +23,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "log" "os" "path/filepath" @@ -33,15 +32,18 @@ import ( "github.com/fsnotify/fsnotify" "github.com/mitchellh/mapstructure" + "github.com/spf13/afero" "github.com/spf13/cast" jww "github.com/spf13/jwalterweatherman" "github.com/spf13/pflag" ) var v *Viper +var fs afero.Fs func init() { v = New() + fs = afero.NewOsFs() } type remoteConfigFactory interface { @@ -936,7 +938,7 @@ func (v *Viper) ReadInConfig() error { return UnsupportedConfigError(v.getConfigType()) } - file, err := ioutil.ReadFile(v.getConfigFile()) + file, err := afero.ReadFile(fs, v.getConfigFile()) if err != nil { return err } @@ -954,7 +956,7 @@ func (v *Viper) MergeInConfig() error { return UnsupportedConfigError(v.getConfigType()) } - file, err := ioutil.ReadFile(v.getConfigFile()) + file, err := afero.ReadFile(fs, v.getConfigFile()) if err != nil { return err }