From c53a6dbc69a5ffb884e8129ab409094248581251 Mon Sep 17 00:00:00 2001 From: Vlad Didenko Date: Wed, 27 May 2015 16:29:08 -0500 Subject: [PATCH] spf13/viper#73 Fix when no config found in specified directories No config in specified directories now behave the same regardless if the config file present in CWD or not --- viper.go | 37 +++++++++++++++++++++++++++++++++---- viper_test.go | 3 ++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/viper.go b/viper.go index 11f3a77..ff80206 100644 --- a/viper.go +++ b/viper.go @@ -72,6 +72,16 @@ func (rce RemoteConfigError) Error() string { return fmt.Sprintf("Remote Configurations Error: %s", string(rce)) } +// Denotes failing to find configuration file. +type ConfigFileNotFoundError struct { + name, locations string +} + +// Returns the formatted configuration error. +func (fnfe ConfigFileNotFoundError) Error() string { + return fmt.Sprintf("Config File %q Not Found in %q", fnfe.name, fnfe.locations) +} + // Viper is a prioritized configuration registry. It // maintains a set of configuration sources, fetches // values to populate those, and provides them according @@ -955,9 +965,22 @@ func (v *Viper) searchInPath(in string) (filename string) { return "" } -// search all configPaths for any config file. -// Returns the first path that exists (and is a config file) +// Choose where to look for a config file: either +// in provided directories or in the working directory func (v *Viper) findConfigFile() (string, error) { + + if len(v.configPaths) > 0 { + return v.findConfigInPaths() + } else { + return v.findConfigInCWD() + } + +} + +// Search all configPaths for any config file. +// Returns the first path that exists (and has a config file) +func (v *Viper) findConfigInPaths() (string, error) { + jww.INFO.Println("Searching for config in ", v.configPaths) for _, cp := range v.configPaths { @@ -966,14 +989,20 @@ func (v *Viper) findConfigFile() (string, error) { return file, nil } } + return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)} +} + +// Search the current working directory for any config file. +func (v *Viper) findConfigInCWD() (string, error) { - // try the current working directory wd, _ := os.Getwd() + jww.INFO.Println("Searching for config in ", wd) + file := v.searchInPath(wd) if file != "" { return file, nil } - return "", fmt.Errorf("config file not found in: %s", v.configPaths) + return "", ConfigFileNotFoundError{v.configName, wd} } // Prints all configuration registries for debugging diff --git a/viper_test.go b/viper_test.go index 554ff7a..2379fcd 100644 --- a/viper_test.go +++ b/viper_test.go @@ -646,7 +646,7 @@ func TestWrongDirsSearchNotFoundHasCWDConfig(t *testing.T) { v.AddConfigPath(`thispathaintthere`) err := v.ReadInConfig() - assert.Nil(t, err) + assert.Equal(t, reflect.TypeOf(UnsupportedConfigError("")), reflect.TypeOf(err)) // Should not see the value "root" which comes from config in CWD assert.Equal(t, `default`, v.GetString(`key`)) @@ -657,6 +657,7 @@ func TestWrongDirsSearchNotFoundNoCWDConfig(t *testing.T) { _, config, cleanup := initDirs(t) defer cleanup() + // Remove the config file in CWD os.Remove(config + ".toml") v := New()