-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglob.go
118 lines (97 loc) · 2.62 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package glob
import (
"os"
"path/filepath"
"github.com/pkg/errors"
"go.uber.org/zap"
)
type GlobPatterns = map[string]*ParsedPattern
type Options struct {
IgnorePatterns []string
IgnoreFiles []string
CWD string
Patterns []string
AbsolutePaths bool
Debug bool
}
func Pattern(pattern string) *Options {
return &Options{
Patterns: []string{pattern},
}
}
// CWD sets a cwd where the glob pattern is executed in by default the process current working directory is used
func CWD(cwd string) *Options {
return &Options{
CWD: cwd,
}
}
// IgnorePatterns sets a list of patterns that is used for ignoring specific files or directories
func IgnorePattern(pattern string) *Options {
return &Options{
IgnorePatterns: []string{pattern},
}
}
// IgnoreFiles provides a list on file paths relative to the current working directory that follow the .gitignore syntax and are used to provide ignore patterns
func IgnoreFile(files string) *Options {
return &Options{
IgnoreFiles: []string{files},
}
}
// Glob is the main glob function that is used to get a list of files in a specific directory matching the patterns and respecting the ignores
func Glob(options ...*Options) ([]string, error) {
// use a map to avoid duplicates
ignores := make(GlobPatterns)
patterns := make(GlobPatterns)
absolutePaths := false
logger := zap.NewNop()
var cwd string
for _, opt := range options {
if len(opt.CWD) > 0 {
cwd = opt.CWD
}
if opt.AbsolutePaths {
absolutePaths = true
}
if opt.Debug {
l, err := zap.NewDevelopment()
if err != nil {
return nil, err
}
logger = l
}
for _, p := range opt.IgnorePatterns {
reg, err := Parse(p)
if err != nil {
return nil, errors.Wrapf(err, "could not parse ignorePattern %s", p)
}
ignores[p] = reg
}
for _, file := range opt.IgnoreFiles {
gitIgnores, err := ParseGitignore(filepath.Join(cwd, file))
if err != nil {
return nil, errors.Wrapf(err, "could not parse ignore file %s", file)
}
for key, val := range gitIgnores {
ignores[key] = val
}
}
for _, p := range opt.Patterns {
reg, err := Parse(p)
if err != nil {
return nil, errors.Wrapf(err, "could not parse provided pattern %s", p)
}
patterns[p] = reg
}
}
if len(patterns) < 1 {
return nil, errors.New("No patterns provided! Please provide a valid glob pattern as parameter")
}
if len(cwd) < 1 {
wd, err := os.Getwd()
if err != nil {
return nil, errors.Wrap(err, "could not determine current working directory, please provide it as argument")
}
cwd = wd
}
return matchFiles(cwd, patterns, ignores, absolutePaths, logger)
}