This repository has been archived by the owner on Apr 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
100 lines (84 loc) · 2.79 KB
/
files.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package sprbox
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
// files type regexp
const extRegexp = `(?i)(.y(|a)ml|.toml|.json)` // `(?i)(\..{3,4})` //
// FILE SEARCH ---------------------------------------------------------------------------------------------------------
// walkConfigPath look for a file matching the passed regex and skipping sub-directories.
func walkConfigPath(configPath string, regex *regexp.Regexp) (matchedFile string) {
err := filepath.Walk(configPath, func(path string, info os.FileInfo, err error) error {
// nil if the path does not exist
if info == nil {
return filepath.SkipDir
}
if info.IsDir() && info.Name() != filepath.Base(configPath) {
return filepath.SkipDir
}
if !info.Mode().IsRegular() {
return nil
}
if regex.MatchString(info.Name()) {
matchedFile = path
}
return nil
})
if err != nil {
fmt.Println(err)
}
return
}
// configFilesByEnv will search for the given file in the given path
// returning all the eligible files (eg.: <path>/myConfig.yaml and <path>/myConfig.<environment>.yaml)
//
// 'files' can also be passed without file extension,
// configFilesByEnv is agnostic and will match any
// supported extension in that case.
//
// The 'file' name will be searched as (in that order):
// - '<path>/<file>(.* || <the_provided_extension>)'
// - '<path>/<file>.<environment>(.* || <the_provided_extension>)'
//
// The latest found files will override previous.
func configFilesByEnv(files ...string) (foundFiles []string) {
for _, file := range files {
configPath, fileName := filepath.Split(file)
if len(configPath) == 0 {
configPath = "./"
}
var regexEnv *regexp.Regexp
var regex *regexp.Regexp
ext := filepath.Ext(fileName)
extTrimmed := strings.TrimSuffix(fileName, ext)
if len(ext) == 0 {
ext = extRegexp
debugPrintf(darkGrey("\nlooking for '%s%s' in '%s'..."), fileName, extRegexp, configPath)
} else {
debugPrintf(darkGrey("\nlooking for '%s' in '%s'..."), fileName, configPath)
}
format := "^%s%s$"
if !fileSearchCaseSensitive {
format = "(?i)(^%s)%s$"
}
regexEnv = regexp.MustCompile(fmt.Sprintf(format, fmt.Sprintf("%s.%s", extTrimmed, Env().ID()), ext))
regex = regexp.MustCompile(fmt.Sprintf(format, extTrimmed, ext))
// look for the config file in the config path (eg.: tool.yml)
if matchedFiles := walkConfigPath(configPath, regex); len(matchedFiles) > 0 {
foundFiles = append(foundFiles, matchedFiles)
}
// look for the env config file in the config path (eg.: tool.development.yml)
if matchedFiles := walkConfigPath(configPath, regexEnv); len(matchedFiles) > 0 {
foundFiles = append(foundFiles, matchedFiles)
}
}
if len(foundFiles) > 0 {
debugPrintf("\n%s %s\n", strings.Join(foundFiles, green(" <- ")), green("="))
} else {
debugPrintf("\n")
}
return
}