Move the Afero fs to the Viper type

And make a exported setter for it.
This commit is contained in:
Bjørn Erik Pedersen 2016-08-05 09:45:58 +02:00
parent d0c2644870
commit a59dcccc82
2 changed files with 13 additions and 5 deletions

View File

@ -77,7 +77,7 @@ func absPathify(inPath string) string {
// Check if File / Directory Exists // Check if File / Directory Exists
func exists(path string) (bool, error) { func exists(path string) (bool, error) {
_, err := fs.Stat(path) _, err := v.fs.Stat(path)
if err == nil { if err == nil {
return true, nil return true, nil
} }

View File

@ -39,11 +39,9 @@ import (
) )
var v *Viper var v *Viper
var fs afero.Fs
func init() { func init() {
v = New() v = New()
fs = afero.NewOsFs()
} }
type remoteConfigFactory interface { type remoteConfigFactory interface {
@ -134,6 +132,9 @@ type Viper struct {
// A set of paths to look for the config file in // A set of paths to look for the config file in
configPaths []string configPaths []string
// The filesystem to read config from.
fs afero.Fs
// A set of remote providers to search for the configuration // A set of remote providers to search for the configuration
remoteProviders []*defaultRemoteProvider remoteProviders []*defaultRemoteProvider
@ -163,6 +164,7 @@ func New() *Viper {
v := new(Viper) v := new(Viper)
v.keyDelim = "." v.keyDelim = "."
v.configName = "config" v.configName = "config"
v.fs = afero.NewOsFs()
v.config = make(map[string]interface{}) v.config = make(map[string]interface{})
v.override = make(map[string]interface{}) v.override = make(map[string]interface{})
v.defaults = make(map[string]interface{}) v.defaults = make(map[string]interface{})
@ -938,7 +940,7 @@ func (v *Viper) ReadInConfig() error {
return UnsupportedConfigError(v.getConfigType()) return UnsupportedConfigError(v.getConfigType())
} }
file, err := afero.ReadFile(fs, v.getConfigFile()) file, err := afero.ReadFile(v.fs, v.getConfigFile())
if err != nil { if err != nil {
return err return err
} }
@ -956,7 +958,7 @@ func (v *Viper) MergeInConfig() error {
return UnsupportedConfigError(v.getConfigType()) return UnsupportedConfigError(v.getConfigType())
} }
file, err := afero.ReadFile(fs, v.getConfigFile()) file, err := afero.ReadFile(v.fs, v.getConfigFile())
if err != nil { if err != nil {
return err return err
} }
@ -1210,6 +1212,12 @@ func (v *Viper) AllSettings() map[string]interface{} {
return m 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. // Name for the config file.
// Does not include extension. // Does not include extension.
func SetConfigName(in string) { v.SetConfigName(in) } func SetConfigName(in string) { v.SetConfigName(in) }