forked from exercism/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrack.go
More file actions
71 lines (62 loc) · 1.39 KB
/
track.go
File metadata and controls
71 lines (62 loc) · 1.39 KB
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 config
import (
"regexp"
"sort"
)
var defaultIgnorePatterns = []string{
".*[.]md",
"[.]?solution[.]json",
}
// Track holds the CLI-related settings for a track.
type Track struct {
ID string
IgnorePatterns []string
ignoreRegexes []*regexp.Regexp
}
// NewTrack provides a track configured with default values.
func NewTrack(id string) *Track {
t := &Track{
ID: id,
}
t.SetDefaults()
return t
}
// SetDefaults configures a track with default values.
func (t *Track) SetDefaults() {
m := map[string]bool{}
for _, pattern := range t.IgnorePatterns {
m[pattern] = true
}
for _, pattern := range defaultIgnorePatterns {
if !m[pattern] {
t.IgnorePatterns = append(t.IgnorePatterns, pattern)
}
}
sort.Strings(t.IgnorePatterns)
}
// AcceptFilename judges a files admissability based on the name.
func (t *Track) AcceptFilename(f string) (bool, error) {
if err := t.CompileRegexes(); err != nil {
return false, err
}
for _, re := range t.ignoreRegexes {
if re.MatchString(f) {
return false, nil
}
}
return true, nil
}
// CompileRegexes precompiles the ignore patterns.
func (t *Track) CompileRegexes() error {
if len(t.ignoreRegexes) == len(t.IgnorePatterns) {
return nil
}
for _, pattern := range t.IgnorePatterns {
re, err := regexp.Compile(pattern)
if err != nil {
return err
}
t.ignoreRegexes = append(t.ignoreRegexes, re)
}
return nil
}