-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
74 lines (63 loc) · 1.45 KB
/
decoder.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
package codeowners
import (
"bufio"
"io"
)
// Decoder providers functionality to read CODEOWNERS data
type Decoder struct {
scanner *bufio.Scanner
line string
lineNo int
done bool
}
// NewDecoder generates a new Decoder instance. The reader should contain the contents of the CODEOWNERS file
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{
scanner: bufio.NewScanner(r),
line: "",
lineNo: 0,
done: false,
}
}
// peek will scan the next line
func (d *Decoder) peek() {
if !d.scanner.Scan() {
d.done = true
return
}
d.line = d.scanner.Text()
line := sanitiseLine(d.line)
d.lineNo++
if len(line) == 0 && !d.done {
d.peek()
}
}
// More returns true if there are available CODEOWNERS lines to be scanned.
// And also advances to the next line.
func (d *Decoder) More() bool {
d.peek()
return !d.done
}
// Token parses the next available line in the CODEOWNERS file.
// If More was never called it will return an empty token.
// After end of file Token will always return the last line.
func (d *Decoder) Token() (Token, int) {
pattern, owners := ParseLine(d.line)
return Token{
path: pattern,
owners: owners,
}, d.lineNo
}
// Token providers reading capabilities for every CODEOWNERS line
type Token struct {
path string
owners []string
}
// Path returns the file path pattern
func (t Token) Path() string {
return t.path
}
// Owners returns the owners
func (t Token) Owners() []string {
return t.owners
}