2023-09-12 00:53:22 +03:00
|
|
|
//go:build finder
|
|
|
|
// +build 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) {
|
2023-09-09 09:28:39 +03:00
|
|
|
var names []string
|
|
|
|
|
|
|
|
if v.configType != "" {
|
2023-09-11 05:55:19 +03:00
|
|
|
names = locafero.NameWithOptionalExtensions(v.configName, SupportedExts...)
|
2023-09-09 09:28:39 +03:00
|
|
|
} else {
|
2023-09-11 05:55:19 +03:00
|
|
|
names = locafero.NameWithExtensions(v.configName, SupportedExts...)
|
2023-09-09 09:28:39 +03:00
|
|
|
}
|
|
|
|
|
2023-09-11 05:55:19 +03:00
|
|
|
finder := locafero.Finder{
|
2023-09-09 09:28:39 +03:00
|
|
|
Paths: v.configPaths,
|
|
|
|
Names: names,
|
2023-09-11 05:55:19 +03:00
|
|
|
Type: locafero.FileTypeFile,
|
2021-09-21 12:08:46 +03:00
|
|
|
}
|
|
|
|
|
2023-09-11 05:55:19 +03:00
|
|
|
results, err := locafero.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
|
|
|
}
|