From a59dcccc828173bb1f58d1650619724b59d18af5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 5 Aug 2016 09:45:58 +0200 Subject: [PATCH] Move the Afero fs to the Viper type And make a exported setter for it. --- util.go | 2 +- viper.go | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/util.go b/util.go index 6b34e14..fe6cb45 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 := 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 @@ import ( ) var v *Viper -var fs afero.Fs func init() { v = New() - fs = afero.NewOsFs() } type remoteConfigFactory interface { @@ -134,6 +132,9 @@ type Viper struct { // 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 @@ func New() *Viper { 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 @@ func (v *Viper) ReadInConfig() error { 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 @@ func (v *Viper) MergeInConfig() error { 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 @@ func (v *Viper) AllSettings() map[string]interface{} { 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) }