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
This commit is contained in:
Vlad Didenko 2015-05-27 16:29:08 -05:00
parent 033e966e68
commit c53a6dbc69
2 changed files with 35 additions and 5 deletions

View file

@ -72,6 +72,16 @@ func (rce RemoteConfigError) Error() string {
return fmt.Sprintf("Remote Configurations Error: %s", string(rce)) 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 // Viper is a prioritized configuration registry. It
// maintains a set of configuration sources, fetches // maintains a set of configuration sources, fetches
// values to populate those, and provides them according // values to populate those, and provides them according
@ -955,9 +965,22 @@ func (v *Viper) searchInPath(in string) (filename string) {
return "" return ""
} }
// search all configPaths for any config file. // Choose where to look for a config file: either
// Returns the first path that exists (and is a config file) // in provided directories or in the working directory
func (v *Viper) findConfigFile() (string, error) { 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) jww.INFO.Println("Searching for config in ", v.configPaths)
for _, cp := range v.configPaths { for _, cp := range v.configPaths {
@ -966,14 +989,20 @@ func (v *Viper) findConfigFile() (string, error) {
return file, nil 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() wd, _ := os.Getwd()
jww.INFO.Println("Searching for config in ", wd)
file := v.searchInPath(wd) file := v.searchInPath(wd)
if file != "" { if file != "" {
return file, nil 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 // Prints all configuration registries for debugging

View file

@ -646,7 +646,7 @@ func TestWrongDirsSearchNotFoundHasCWDConfig(t *testing.T) {
v.AddConfigPath(`thispathaintthere`) v.AddConfigPath(`thispathaintthere`)
err := v.ReadInConfig() 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 // Should not see the value "root" which comes from config in CWD
assert.Equal(t, `default`, v.GetString(`key`)) assert.Equal(t, `default`, v.GetString(`key`))
@ -657,6 +657,7 @@ func TestWrongDirsSearchNotFoundNoCWDConfig(t *testing.T) {
_, config, cleanup := initDirs(t) _, config, cleanup := initDirs(t)
defer cleanup() defer cleanup()
// Remove the config file in CWD
os.Remove(config + ".toml") os.Remove(config + ".toml")
v := New() v := New()