2024-06-03 07:32:13 +00:00
|
|
|
//go:build !viper_finder
|
2021-09-21 09:08:46 +00:00
|
|
|
|
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Search all configPaths for any config file.
|
|
|
|
// Returns the first path that exists (and is a config file).
|
|
|
|
func (v *Viper) findConfigFile() (string, error) {
|
2021-12-09 17:36:49 +00:00
|
|
|
v.logger.Info("searching for config in paths", "paths", v.configPaths)
|
2021-09-21 09:08:46 +00:00
|
|
|
|
|
|
|
for _, cp := range v.configPaths {
|
|
|
|
file := v.searchInPath(cp)
|
|
|
|
if file != "" {
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Viper) searchInPath(in string) (filename string) {
|
2021-12-09 17:36:49 +00:00
|
|
|
v.logger.Debug("searching for config in path", "path", in)
|
2021-09-21 09:08:46 +00:00
|
|
|
for _, ext := range SupportedExts {
|
2021-12-09 17:36:49 +00:00
|
|
|
v.logger.Debug("checking if file exists", "file", filepath.Join(in, v.configName+"."+ext))
|
2021-09-21 09:08:46 +00:00
|
|
|
if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {
|
2021-12-09 17:36:49 +00:00
|
|
|
v.logger.Debug("found file", "file", filepath.Join(in, v.configName+"."+ext))
|
2021-09-21 09:08:46 +00:00
|
|
|
return filepath.Join(in, v.configName+"."+ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.configType != "" {
|
|
|
|
if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b {
|
|
|
|
return filepath.Join(in, v.configName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-10-09 14:52:53 +00:00
|
|
|
// exists checks if file exists.
|
2021-09-21 09:08:46 +00:00
|
|
|
func exists(fs afero.Fs, path string) (bool, error) {
|
|
|
|
stat, err := fs.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return !stat.IsDir(), nil
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|