forked from coredns/coredns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
owners_generate.go
91 lines (80 loc) · 1.61 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
90
91
//+build ignore
// generates plugin/chaos/zowners.go.
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"sort"
"gopkg.in/yaml.v2"
)
func main() {
o := map[string]struct{}{}
// top-level OWNERS file
o, err := owners("OWNERS", o)
if err != nil {
log.Fatal(err)
}
// each of the plugins, in case someone is not in the top-level one
err = filepath.Walk("plugin",
func(p string, i os.FileInfo, err error) error {
if err != nil {
return err
}
if i.IsDir() {
return nil
}
if path.Base(p) != "OWNERS" {
return nil
}
o, err = owners(p, o)
return err
})
// sort it and format it
list := []string{}
for k := range o {
list = append(list, k)
}
sort.Strings(list)
golist := `package chaos
// Owners are all GitHub handlers of all maintainers.
var Owners = []string{`
c := ", "
for i, a := range list {
if i == len(list)-1 {
c = "}"
}
golist += fmt.Sprintf("%q%s", a, c)
}
if err := ioutil.WriteFile("plugin/chaos/zowners.go", []byte(golist), 0644); err != nil {
log.Fatal(err)
}
return
}
// owners parses a owner file without knowning a whole lot about its structure.
func owners(path string, owners map[string]struct{}) (map[string]struct{}, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
c := yaml.MapSlice{}
err = yaml.Unmarshal(file, &c)
if err != nil {
return nil, err
}
for _, mi := range c {
key, ok := mi.Key.(string)
if !ok {
continue
}
if key == "approvers" {
for _, k := range mi.Value.([]interface{}) {
owners[k.(string)] = struct{}{}
}
}
}
return owners, nil
}