aboutsummaryrefslogtreecommitdiff
path: root/plugin_generate.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2016-08-19 17:14:17 -0700
committerGravatar GitHub <noreply@github.com> 2016-08-19 17:14:17 -0700
commit9ac3cab1b7b1b1e78f86ce3c6a80fbee312162e6 (patch)
tree437e9755927c33af16276ad2602a6da115f948cb /plugin_generate.go
parenta1989c35231b0e5ea271b2f68d82c1a63e697cd0 (diff)
downloadcoredns-9ac3cab1b7b1b1e78f86ce3c6a80fbee312162e6.tar.gz
coredns-9ac3cab1b7b1b1e78f86ce3c6a80fbee312162e6.tar.zst
coredns-9ac3cab1b7b1b1e78f86ce3c6a80fbee312162e6.zip
Make CoreDNS a server type plugin for Caddy (#220)
* Make CoreDNS a server type plugin for Caddy Remove code we don't need and port all middleware over. Fix all tests and rework the documentation. Also make `go generate` build a caddy binary which we then copy into our directory. This means `go build`-builds remain working as-is. And new etc instances in each etcd test for better isolation. Fix more tests and rework test.Server with the newer support Caddy offers. Fix Makefile to support new mode of operation.
Diffstat (limited to 'plugin_generate.go')
-rw-r--r--plugin_generate.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/plugin_generate.go b/plugin_generate.go
new file mode 100644
index 000000000..fe8fef75d
--- /dev/null
+++ b/plugin_generate.go
@@ -0,0 +1,81 @@
+//+build ignore
+
+package main
+
+import (
+ "bytes"
+ "errors"
+ "go/ast"
+ "go/parser"
+ "go/printer"
+ "go/token"
+ "io/ioutil"
+ "log"
+ "strconv"
+)
+
+func AddImportToFile(file, imprt string) ([]byte, error) {
+ fset := token.NewFileSet()
+ f, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
+ if err != nil {
+ return nil, err
+ }
+
+ for _, s := range f.Imports {
+ iSpec := &ast.ImportSpec{Path: &ast.BasicLit{Value: s.Path.Value}}
+ if iSpec.Path.Value == strconv.Quote(imprt) {
+ return nil, errors.New("coredns import already found")
+ }
+ }
+
+ for i := 0; i < len(f.Decls); i++ {
+ d := f.Decls[i]
+
+ switch d.(type) {
+ case *ast.FuncDecl:
+ // No action
+ case *ast.GenDecl:
+ dd := d.(*ast.GenDecl)
+
+ // IMPORT Declarations
+ if dd.Tok == token.IMPORT {
+ // Add the new import
+ iSpec := &ast.ImportSpec{Name: &ast.Ident{Name: "_"}, Path: &ast.BasicLit{Value: strconv.Quote(imprt)}}
+ dd.Specs = append(dd.Specs, iSpec)
+ break
+ }
+ }
+ }
+
+ ast.SortImports(fset, f)
+
+ out, err := GenerateFile(fset, f)
+ return out, err
+}
+
+func GenerateFile(fset *token.FileSet, file *ast.File) ([]byte, error) {
+ var output []byte
+ buffer := bytes.NewBuffer(output)
+ if err := printer.Fprint(buffer, fset, file); err != nil {
+ return nil, err
+ }
+
+ return buffer.Bytes(), nil
+}
+
+const (
+ coredns = "github.com/miekg/coredns/core"
+ // If everything is OK and we are sitting in CoreDNS' dir, this is where run.go should be.
+ caddyrun = "../../mholt/caddy/caddy/caddymain/run.go"
+)
+
+func main() {
+ out, err := AddImportToFile(caddyrun, coredns)
+ if err != nil {
+ log.Printf("failed to add import: %s", err)
+ return
+ }
+ if err := ioutil.WriteFile(caddyrun, out, 0644); err != nil {
+ log.Fatalf("failed to write go file: %s", err)
+ }
+}