Skip to content

Commit 34e56bc

Browse files
authored
Rename package files (#5)
* Move example into test file * Rename decoder receiver * Rename package files
1 parent 0c55fca commit 34e56bc

4 files changed

Lines changed: 102 additions & 105 deletions

File tree

codeowners.go

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,5 @@
11
// Package codeowners provides funcionality to evaluate CODEOWNERS file.
22
package codeowners // import "github.com/fmenezes/codeowners"
33

4-
import (
5-
"bufio"
6-
"io"
7-
"strings"
8-
)
9-
104
// DefaultLocations provides default locations for the CODEOWNERS file
115
var DefaultLocations = [...]string{"CODEOWNERS", "docs/CODEOWNERS", ".github/CODEOWNERS"}
12-
13-
// Decoder providers functionality to read CODEOWNERS data
14-
type Decoder struct {
15-
scanner *bufio.Scanner
16-
line string
17-
done bool
18-
}
19-
20-
// NewDecoder generates a new CodeOwnersScanner instance. The reader should be a reader containing the contents of the CODEOWNERS file
21-
func NewDecoder(r io.Reader) *Decoder {
22-
return &Decoder{
23-
scanner: bufio.NewScanner(r),
24-
line: "",
25-
done: false,
26-
}
27-
}
28-
29-
// peek will scan the next line
30-
func (s *Decoder) peek() {
31-
if !s.scanner.Scan() {
32-
s.done = true
33-
return
34-
}
35-
line := sanitiseLine(s.scanner.Text())
36-
s.line = line
37-
if len(s.line) == 0 && !s.done {
38-
s.peek()
39-
}
40-
}
41-
42-
// sanitiseLine removes all empty space and comments from a given line
43-
func sanitiseLine(line string) string {
44-
i := strings.Index(line, "#")
45-
if i >= 0 {
46-
line = line[:i]
47-
}
48-
return strings.Trim(line, " ")
49-
}
50-
51-
// More returns true if there are available CODEOWNERS lines to be scanned.
52-
// And also advances to the next line.
53-
func (s *Decoder) More() bool {
54-
s.peek()
55-
return !s.done
56-
}
57-
58-
// Token parses the next available line in the CODEOWNERS file.
59-
// If More was never called it will return an empty token.
60-
// After end of file Token will always return the last line.
61-
func (s *Decoder) Token() Token {
62-
line := strings.ReplaceAll(s.line, "\\ ", "\\s")
63-
64-
data := strings.Split(line, " ")
65-
66-
for i := range data {
67-
data[i] = strings.ReplaceAll(data[i], "\\s", " ")
68-
}
69-
70-
return Token{
71-
path: data[0],
72-
owners: data[1:],
73-
}
74-
}
75-
76-
// Token providers reading capabilities for every CODEOWNERS line
77-
type Token struct {
78-
path string
79-
owners []string
80-
}
81-
82-
// Path returns the file path pattern
83-
func (t Token) Path() string {
84-
return t.path
85-
}
86-
87-
// Owners returns the owners
88-
func (t Token) Owners() []string {
89-
return t.owners
90-
}

decoder.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package codeowners
2+
3+
import (
4+
"bufio"
5+
"io"
6+
"strings"
7+
)
8+
9+
// Decoder providers functionality to read CODEOWNERS data
10+
type Decoder struct {
11+
scanner *bufio.Scanner
12+
line string
13+
done bool
14+
}
15+
16+
// NewDecoder generates a new CodeOwnersScanner instance. The reader should be a reader containing the contents of the CODEOWNERS file
17+
func NewDecoder(r io.Reader) *Decoder {
18+
return &Decoder{
19+
scanner: bufio.NewScanner(r),
20+
line: "",
21+
done: false,
22+
}
23+
}
24+
25+
// peek will scan the next line
26+
func (d *Decoder) peek() {
27+
if !d.scanner.Scan() {
28+
d.done = true
29+
return
30+
}
31+
line := sanitiseLine(d.scanner.Text())
32+
d.line = line
33+
if len(d.line) == 0 && !d.done {
34+
d.peek()
35+
}
36+
}
37+
38+
// sanitiseLine removes all empty space and comments from a given line
39+
func sanitiseLine(line string) string {
40+
i := strings.Index(line, "#")
41+
if i >= 0 {
42+
line = line[:i]
43+
}
44+
return strings.Trim(line, " ")
45+
}
46+
47+
// More returns true if there are available CODEOWNERS lines to be scanned.
48+
// And also advances to the next line.
49+
func (d *Decoder) More() bool {
50+
d.peek()
51+
return !d.done
52+
}
53+
54+
// Token parses the next available line in the CODEOWNERS file.
55+
// If More was never called it will return an empty token.
56+
// After end of file Token will always return the last line.
57+
func (d *Decoder) Token() Token {
58+
line := strings.ReplaceAll(d.line, "\\ ", "\\s")
59+
60+
data := strings.Split(line, " ")
61+
62+
for i := range data {
63+
data[i] = strings.ReplaceAll(data[i], "\\s", " ")
64+
}
65+
66+
return Token{
67+
path: data[0],
68+
owners: data[1:],
69+
}
70+
}
71+
72+
// Token providers reading capabilities for every CODEOWNERS line
73+
type Token struct {
74+
path string
75+
owners []string
76+
}
77+
78+
// Path returns the file path pattern
79+
func (t Token) Path() string {
80+
return t.path
81+
}
82+
83+
// Owners returns the owners
84+
func (t Token) Owners() []string {
85+
return t.owners
86+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codeowners_test
22

33
import (
4+
"fmt"
45
"reflect"
56
"strings"
67
"testing"
@@ -121,3 +122,18 @@ func TestMoreNotCalled(t *testing.T) {
121122
t.Error("Owners should be empty")
122123
}
123124
}
125+
126+
func ExampleDecoder() {
127+
decoder := codeowners.NewDecoder(strings.NewReader(`* test@example.org
128+
filepattern @owner`))
129+
for decoder.More() {
130+
token := decoder.Token()
131+
fmt.Printf("File Pattern: %s\n", token.Path())
132+
fmt.Printf("Owners: %v\n", token.Owners())
133+
}
134+
// Output:
135+
// File Pattern: *
136+
// Owners: [test@example.org]
137+
// File Pattern: filepattern
138+
// Owners: [@owner]
139+
}

example_codeowners_test.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)