| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | //+build ignore
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"bufio"
 | 
					
						
							|  |  |  | 	"go/format"
 | 
					
						
							|  |  |  | 	"io/ioutil"
 | 
					
						
							|  |  |  | 	"log"
 | 
					
						
							|  |  |  | 	"os"
 | 
					
						
							|  |  |  | 	"sort"
 | 
					
						
							|  |  |  | 	"strconv"
 | 
					
						
							|  |  |  | 	"strings"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() {
 | 
					
						
							|  |  |  | 	mi := make(map[string]string, 0)
 | 
					
						
							|  |  |  | 	md := make(map[int]string, 0)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	file, err := os.Open(pluginFile)
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 	fatalIfErr(err)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	defer file.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	scanner := bufio.NewScanner(file)
 | 
					
						
							|  |  |  | 	for scanner.Scan() {
 | 
					
						
							|  |  |  | 		line := scanner.Text()
 | 
					
						
							| 
									
										
										
										
											2017-02-19 20:34:29 +00:00
										 |  |  | 		if strings.HasPrefix(line, "#") {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		items := strings.Split(line, ":")
 | 
					
						
							|  |  |  | 		if len(items) != 3 {
 | 
					
						
							|  |  |  | 			// ignore
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		priority, err := strconv.Atoi(items[0])
 | 
					
						
							|  |  |  | 		fatalIfErr(err)
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 		if v, ok := md[priority]; ok {
 | 
					
						
							|  |  |  | 			log.Fatalf("Duplicate priority '%d', slot already taken by %q", priority, v)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-02-19 20:34:29 +00:00
										 |  |  | 		md[priority] = items[1]
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		mi[items[1]] = pluginPath + items[2] // Default, unless overridden by 3rd arg
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		if _, err := os.Stat(pluginFSPath + items[2]); err != nil { // External package has been given
 | 
					
						
							| 
									
										
										
										
											2017-02-19 20:34:29 +00:00
										 |  |  | 			mi[items[1]] = items[2]
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	genImports("core/zplugin.go", "core", mi)
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 	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"
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-08 04:27:57 -07:00
										 |  |  | 	outs += "// Include all plugins.\n"
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 	for _, v := range mi {
 | 
					
						
							| 
									
										
										
										
											2017-02-15 11:38:55 -05:00
										 |  |  | 		outs += `_ "` + v + `"` + "\n"
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	outs += ")\n"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res, err := format.Source([]byte(outs))
 | 
					
						
							|  |  |  | 	fatalIfErr(err)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = ioutil.WriteFile(file, res, 0644)
 | 
					
						
							|  |  |  | 	fatalIfErr(err)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func genDirectives(file, pack string, md map[int]string) {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	outs := header + "package " + pack + "\n\n"
 | 
					
						
							|  |  |  | 	outs += `
 | 
					
						
							|  |  |  | // Directives are registered in the order they should be
 | 
					
						
							|  |  |  | // executed.
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Ordering is VERY important. Every plugin will
 | 
					
						
							|  |  |  | // feel the effects of all other plugin below
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | // (after) them during a request, but they must not
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // care what plugin above them are doing.
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | var directives = []string{
 | 
					
						
							|  |  |  | `
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var orders []int
 | 
					
						
							|  |  |  | 	for k := range md {
 | 
					
						
							|  |  |  | 		orders = append(orders, k)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	sort.Ints(orders)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, k := range orders {
 | 
					
						
							|  |  |  | 		outs += `"` + md[k] + `",` + "\n"
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	outs += "}\n"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res, err := format.Source([]byte(outs))
 | 
					
						
							|  |  |  | 	fatalIfErr(err)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = ioutil.WriteFile(file, res, 0644)
 | 
					
						
							|  |  |  | 	fatalIfErr(err)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func fatalIfErr(err error) {
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		log.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	pluginPath   = "github.com/coredns/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"
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | )
 |