mirror of
https://github.com/spf13/viper
synced 2024-12-23 12:07:02 +00:00
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:
parent
033e966e68
commit
c53a6dbc69
2 changed files with 35 additions and 5 deletions
37
viper.go
37
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
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue