| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | //+build ignore
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"bufio"
 | 
					
						
							|  |  |  | 	"go/format"
 | 
					
						
							|  |  |  | 	"io/ioutil"
 | 
					
						
							|  |  |  | 	"log"
 | 
					
						
							|  |  |  | 	"os"
 | 
					
						
							|  |  |  | 	"strings"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() {
 | 
					
						
							|  |  |  | 	mi := make(map[string]string, 0)
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 	md := []string{}
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	file, err := os.Open(pluginFile)
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		log.Fatalf("Failed to open %s: %q", pluginFile, err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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, ":")
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 		if len(items) != 2 {
 | 
					
						
							|  |  |  | 			// ignore empty lines
 | 
					
						
							| 
									
										
										
										
											2017-02-19 20:34:29 +00:00
										 |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 		name, repo := items[0], items[1]
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 		if _, ok := mi[name]; ok {
 | 
					
						
							|  |  |  | 			log.Fatalf("Duplicate entry %q", name)
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 		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
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 13:54:10 -08:00
										 |  |  | 	genImports("core/plugin/zplugin.go", "plugin", 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"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 	if err := formatAndWrite(file, outs); err != nil {
 | 
					
						
							|  |  |  | 		log.Fatalf("Failed to format and write: %q", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | func genDirectives(file, pack string, md []string) {
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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-12-15 01:26:36 -06:00
										 |  |  | var Directives = []string{
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | `
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 	for i := range md {
 | 
					
						
							|  |  |  | 		outs += `"` + md[i] + `",` + "\n"
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	outs += "}\n"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 	if err := formatAndWrite(file, outs); err != nil {
 | 
					
						
							|  |  |  | 		log.Fatalf("Failed to format and write: %q", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | func formatAndWrite(file string, data string) error {
 | 
					
						
							|  |  |  | 	res, err := format.Source([]byte(data))
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 		return err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err = ioutil.WriteFile(file, res, 0644); err != nil {
 | 
					
						
							|  |  |  | 		return err
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 	return nil
 | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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
										 |  |  | )
 |