forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glob.go
71 lines (67 loc) · 1.84 KB
/
glob.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
package file
import (
"fmt"
"path/filepath"
)
func wildcards(doubleStarPatternDepth uint8, dir string, suffix string) []string {
wildcardList := []string{}
w := ""
i := uint8(0)
if dir == "" && suffix == "" {
// Don't expand to "" on relative paths
w = "*"
i = 1
}
for ; i <= doubleStarPatternDepth; i++ {
wildcardList = append(wildcardList, w)
w = filepath.Join(w, "*")
}
return wildcardList
}
// GlobPatterns detects the use of "**" and expands it to standard glob patterns up to a max depth
func GlobPatterns(pattern string, doubleStarPatternDepth uint8) ([]string, error) {
if doubleStarPatternDepth == 0 {
return []string{pattern}, nil
}
var wildcardList []string
var prefix string
var suffix string
dir, file := filepath.Split(filepath.Clean(pattern))
for file != "" && file != "." {
if file == "**" {
if len(wildcardList) > 0 {
return nil, fmt.Errorf("multiple ** in %q", pattern)
}
wildcardList = wildcards(doubleStarPatternDepth, dir, suffix)
prefix = dir
} else if len(wildcardList) == 0 {
suffix = filepath.Join(file, suffix)
}
dir, file = filepath.Split(filepath.Clean(dir))
}
if len(wildcardList) == 0 {
return []string{pattern}, nil
}
var patterns []string
for _, w := range wildcardList {
patterns = append(patterns, filepath.Join(prefix, w, suffix))
}
return patterns, nil
}
// Glob expands '**' patterns into multiple patterns to satisfy https://golang.org/pkg/path/filepath/#Match
func Glob(pattern string, doubleStarPatternDepth uint8) ([]string, error) {
patterns, err := GlobPatterns(pattern, doubleStarPatternDepth)
if err != nil {
return nil, err
}
var matches []string
for _, p := range patterns {
// Evaluate the path as a wildcards/shell glob
match, err := filepath.Glob(p)
if err != nil {
return nil, err
}
matches = append(matches, match...)
}
return matches, nil
}