forked from coredns/coredns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectives_generate.go
More file actions
135 lines (108 loc) · 2.78 KB
/
directives_generate.go
File metadata and controls
135 lines (108 loc) · 2.78 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//go:build ignore
package main
import (
"bufio"
"go/format"
"log"
"os"
"strings"
)
func main() {
mi := make(map[string]string, 0)
md := []string{}
parsePlugin := func(element string) {
items := strings.Split(element, ":")
if len(items) != 2 {
// ignore empty lines
return
}
name, repo := items[0], items[1]
if _, ok := mi[name]; ok {
log.Fatalf("Duplicate entry %q", name)
}
md = append(md, name)
mi[name] = pluginPath + repo // Default, unless overridden by 3rd arg
if _, err := os.Stat(pluginFSPath + repo); err != nil { // External package has been given
mi[name] = repo
}
}
file, err := os.Open(pluginFile)
if err != nil {
log.Fatalf("Failed to open %s: %q", pluginFile, err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
parsePlugin(line)
}
for _, element := range strings.Split(os.Getenv("COREDNS_PLUGINS"), ",") {
parsePlugin(element)
}
genImports("core/plugin/zplugin.go", "plugin", mi)
genDirectives("core/dnsserver/zdirectives.go", "dnsserver", md)
}
func genImports(file, pack string, mi map[string]string) {
outs := header + "package " + pack + "\n\n" + "import ("
if len(mi) > 0 {
outs += "\n"
}
coreDnsImports := ""
thirdPartyImports := ""
outs += "// Include all plugins.\n"
for _, v := range mi {
if strings.HasPrefix(v, githubOrg) {
coreDnsImports += `_ "` + v + `"` + "\n"
} else {
thirdPartyImports += `_ "` + v + `"` + "\n"
}
}
outs += coreDnsImports
if thirdPartyImports != "" {
outs += "\n" + thirdPartyImports
}
outs += ")\n"
if err := formatAndWrite(file, outs); err != nil {
log.Fatalf("Failed to format and write: %q", err)
}
}
func genDirectives(file, pack string, md []string) {
outs := header + "package " + pack + "\n\n"
outs += `
// Directives are registered in the order they should be
// executed.
//
// Ordering is VERY important. Every plugin will
// feel the effects of all other plugin below
// (after) them during a request, but they must not
// care what plugin above them are doing.
var Directives = []string{
`
for i := range md {
outs += `"` + md[i] + `",` + "\n"
}
outs += "}\n"
if err := formatAndWrite(file, outs); err != nil {
log.Fatalf("Failed to format and write: %q", err)
}
}
func formatAndWrite(file string, data string) error {
res, err := format.Source([]byte(data))
if err != nil {
return err
}
if err = os.WriteFile(file, res, 0644); err != nil {
return err
}
return nil
}
const (
githubOrg = "github.com/coredns"
pluginPath = githubOrg + "/coredns/plugin/"
pluginFile = "plugin.cfg"
pluginFSPath = "plugin/" // Where the plugins are located on the file system
header = "// generated by directives_generate.go; DO NOT EDIT\n\n"
)