New static configuration loading system.
Co-authored-by: Mathieu Lonjaret <mathieu.lonjaret@gmail.com>
This commit is contained in:
parent
d18edd6f77
commit
8d7eccad5d
165 changed files with 10894 additions and 6076 deletions
50
pkg/cli/file_finder.go
Normal file
50
pkg/cli/file_finder.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package cli
|
||||
|
||||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue