-
Notifications
You must be signed in to change notification settings - Fork 0
/
quack.go
48 lines (38 loc) · 851 Bytes
/
quack.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
package main
import (
"bufio"
"fmt"
"io"
"strings"
)
func ParseQuack(r io.Reader) ([]Matcher, []string, error) {
m := []Matcher{}
paths := []string{}
line := 0
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line++
text := scanner.Text()
if text == "" || text[0] == '#' {
continue
}
matcher, pattern, ok := strings.Cut(text, "\t")
if !ok {
return nil, nil, fmt.Errorf("line %d: missing tab delimiter", line)
}
switch matcher {
case "static":
m = append(m, NewStaticMatcher(pattern))
case "glob":
m = append(m, NewGlobMatcher(pattern))
case "regex":
m = append(m, NewRegexpMatcher(pattern))
case "force":
paths = append(paths, pattern)
default:
return nil, nil, fmt.Errorf("line %d: unknown matcher %q", line, matcher)
}
}
return m, paths, nil
}