forked from coredns/coredns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
owners_generate.go
89 lines (80 loc) · 1.59 KB
/
owners_generate.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
//go:build ignore
// generates plugin/chaos/zowners.go.
package main
import (
"bufio"
"fmt"
"log"
"os"
"sort"
"strings"
)
func main() {
// top-level OWNERS file
o, err := owners("CODEOWNERS")
if err != nil {
log.Fatal(err)
}
golist := `package chaos
// Owners are all GitHub handlers of all maintainers.
var Owners = []string{`
c := ", "
for i, a := range o {
if i == len(o)-1 {
c = "}"
}
golist += fmt.Sprintf("%q%s", a, c)
}
// to prevent `No newline at end of file` with gofmt
golist += "\n"
if err := os.WriteFile("plugin/chaos/zowners.go", []byte(golist), 0644); err != nil {
log.Fatal(err)
}
return
}
func owners(path string) ([]string, error) {
// simple line, by line based format
//
// # In this example, @doctocat owns any files in the build/logs
// # directory at the root of the repository and any of its
// # subdirectories.
// /build/logs/ @doctocat
f, err := os.Open(path)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(f)
users := map[string]struct{}{}
for scanner.Scan() {
text := scanner.Text()
if len(text) == 0 {
continue
}
if text[0] == '#' {
continue
}
ele := strings.Fields(text)
if len(ele) == 0 {
continue
}
// ok ele[0] is the path, the rest are (in our case) github usernames prefixed with @
for _, s := range ele[1:] {
if len(s) <= 1 {
continue
}
users[s[1:]] = struct{}{}
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
u := []string{}
for k := range users {
if strings.HasPrefix(k, "@") {
k = k[1:]
}
u = append(u, k)
}
sort.Strings(u)
return u, nil
}