2024-06-03 10:32:13 +03:00
|
|
|
//go:build viper_finder
|
2021-09-21 12:08:46 +03:00
|
|
|
|
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-09-11 05:55:19 +03:00
|
|
|
"github.com/sagikazarmark/locafero"
|
2021-09-21 12:08:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Search all configPaths for any config file.
|
|
|
|
// Returns the first path that exists (and is a config file).
|
|
|
|
func (v *Viper) findConfigFile() (string, error) {
|
2024-06-03 10:32:13 +03:00
|
|
|
finder := v.finder
|
|
|
|
|
|
|
|
if finder == nil {
|
|
|
|
var names []string
|
|
|
|
|
|
|
|
if v.configType != "" {
|
|
|
|
names = locafero.NameWithOptionalExtensions(v.configName, SupportedExts...)
|
|
|
|
} else {
|
|
|
|
names = locafero.NameWithExtensions(v.configName, SupportedExts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
finder = locafero.Finder{
|
|
|
|
Paths: v.configPaths,
|
|
|
|
Names: names,
|
|
|
|
Type: locafero.FileTypeFile,
|
|
|
|
}
|
2021-09-21 12:08:46 +03:00
|
|
|
}
|
|
|
|
|
2023-09-12 01:06:03 +03:00
|
|
|
results, err := finder.Find(v.fs)
|
2021-09-21 12:08:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-09-09 09:28:39 +03:00
|
|
|
if len(results) == 0 {
|
2021-09-21 12:08:46 +03:00
|
|
|
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
|
|
|
|
}
|
|
|
|
|
2023-09-09 09:28:39 +03:00
|
|
|
return results[0], nil
|
2021-09-21 12:08:46 +03:00
|
|
|
}
|