Move the Afero fs to the Viper type
And make a exported setter for it.
diff --git a/util.go b/util.go
index 6b34e14..fe6cb45 100644
--- a/util.go
+++ b/util.go
@@ -77,7 +77,7 @@
// Check if File / Directory Exists
func exists(path string) (bool, error) {
- _, err := fs.Stat(path)
+ _, err := v.fs.Stat(path)
if err == nil {
return true, nil
}
diff --git a/viper.go b/viper.go
index e39a564..cdf2113 100644
--- a/viper.go
+++ b/viper.go
@@ -39,11 +39,9 @@
)
var v *Viper
-var fs afero.Fs
func init() {
v = New()
- fs = afero.NewOsFs()
}
type remoteConfigFactory interface {
@@ -134,6 +132,9 @@
// A set of paths to look for the config file in
configPaths []string
+ // The filesystem to read config from.
+ fs afero.Fs
+
// A set of remote providers to search for the configuration
remoteProviders []*defaultRemoteProvider
@@ -163,6 +164,7 @@
v := new(Viper)
v.keyDelim = "."
v.configName = "config"
+ v.fs = afero.NewOsFs()
v.config = make(map[string]interface{})
v.override = make(map[string]interface{})
v.defaults = make(map[string]interface{})
@@ -938,7 +940,7 @@
return UnsupportedConfigError(v.getConfigType())
}
- file, err := afero.ReadFile(fs, v.getConfigFile())
+ file, err := afero.ReadFile(v.fs, v.getConfigFile())
if err != nil {
return err
}
@@ -956,7 +958,7 @@
return UnsupportedConfigError(v.getConfigType())
}
- file, err := afero.ReadFile(fs, v.getConfigFile())
+ file, err := afero.ReadFile(v.fs, v.getConfigFile())
if err != nil {
return err
}
@@ -1210,6 +1212,12 @@
return m
}
+// Se the filesystem to use to read configuration.
+func SetFs(fs afero.Fs) { v.SetFs(fs) }
+func (v *Viper) SetFs(fs afero.Fs) {
+ v.fs = fs
+}
+
// Name for the config file.
// Does not include extension.
func SetConfigName(in string) { v.SetConfigName(in) }