mirror of
https://github.com/spf13/viper
synced 2024-12-23 12:07:02 +00:00
Switch between provided and not provided paths.
Refactored config file search to be more explicit about where to look for configuration file
This commit is contained in:
parent
3f164ab4ca
commit
0776ded240
1 changed files with 20 additions and 7 deletions
27
viper.go
27
viper.go
|
@ -955,9 +955,20 @@ 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.findConfigInWD()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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,18 +977,20 @@ func (v *Viper) findConfigFile() (string, error) {
|
||||||
return file, nil
|
return file, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return "", fmt.Errorf("config file not found in: %s", v.configPaths)
|
||||||
|
}
|
||||||
|
|
||||||
if len(v.configPaths) > 0 {
|
// Search the current working directory for any config file.
|
||||||
return "", fmt.Errorf("Config file not found in: %s", v.configPaths)
|
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 the current working directory %q", wd)
|
return "", fmt.Errorf("config file not found in current working directory", v.configPaths)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints all configuration registries for debugging
|
// Prints all configuration registries for debugging
|
||||||
|
|
Loading…
Reference in a new issue