-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
config_finder.go
75 lines (65 loc) · 1.61 KB
/
config_finder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package finder
import (
"errors"
"path/filepath"
"github.com/aquaproj/aqua/pkg/util"
"github.com/spf13/afero"
"github.com/suzuki-shunsuke/go-findconfig/findconfig"
)
var ErrConfigFileNotFound = errors.New("configuration file isn't found")
type ConfigFinder struct {
fs afero.Fs
}
func NewConfigFinder(fs afero.Fs) *ConfigFinder {
return &ConfigFinder{
fs: fs,
}
}
func ParseGlobalConfigFilePaths(env string) []string {
src := filepath.SplitList(env)
paths := make([]string, 0, len(src))
m := make(map[string]struct{}, len(src))
for _, s := range src {
if s == "" {
continue
}
if _, ok := m[s]; ok {
continue
}
m[s] = struct{}{}
paths = append(paths, s)
}
return paths
}
func configFileNames() []string {
return []string{"aqua.yaml", "aqua.yml", ".aqua.yaml", ".aqua.yml"}
}
func (finder *ConfigFinder) Find(wd, configFilePath string, globalConfigFilePaths ...string) (string, error) {
if configFilePath != "" {
return util.Abs(wd, configFilePath), nil
}
configFilePath = findconfig.Find(wd, finder.exist, configFileNames()...)
if configFilePath != "" {
return configFilePath, nil
}
for _, p := range globalConfigFilePaths {
if _, err := finder.fs.Stat(p); err != nil {
continue
}
return p, nil
}
return "", ErrConfigFileNotFound
}
func (finder *ConfigFinder) Finds(wd, configFilePath string) []string {
if configFilePath == "" {
return findconfig.Finds(wd, finder.exist, configFileNames()...)
}
return []string{util.Abs(wd, configFilePath)}
}
func (finder *ConfigFinder) exist(p string) bool {
b, err := afero.Exists(finder.fs, p)
if err != nil {
return false
}
return b
}