mirror of
https://github.com/crazy-max/diun.git
synced 2024-12-22 19:38:28 +00:00
51 lines
893 B
Go
51 lines
893 B
Go
package gonfig
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// Finder holds a list of file paths.
|
|
type Finder struct {
|
|
BasePaths []string
|
|
Extensions []string
|
|
}
|
|
|
|
// Find returns the first valid existing file among configFile
|
|
// and the paths already registered with Finder.
|
|
func (f Finder) Find(configFile string) (string, error) {
|
|
paths := f.getPaths(configFile)
|
|
|
|
for _, filePath := range paths {
|
|
fp := os.ExpandEnv(filePath)
|
|
|
|
_, err := os.Stat(fp)
|
|
if os.IsNotExist(err) {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return filepath.Abs(fp)
|
|
}
|
|
|
|
return "", nil
|
|
}
|
|
|
|
func (f Finder) getPaths(configFile string) []string {
|
|
var paths []string
|
|
if strings.TrimSpace(configFile) != "" {
|
|
paths = append(paths, configFile)
|
|
}
|
|
|
|
for _, basePath := range f.BasePaths {
|
|
for _, ext := range f.Extensions {
|
|
paths = append(paths, basePath+"."+ext)
|
|
}
|
|
}
|
|
|
|
return paths
|
|
}
|