| 
									
										
										
										
											2022-03-18 14:11:14 +00:00
										 |  |  | //go:build ignore | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bufio" | 
					
						
							|  |  |  | 	"go/format" | 
					
						
							|  |  |  | 	"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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-20 21:02:52 -07:00
										 |  |  | 	parsePlugin := func(element string) { | 
					
						
							|  |  |  | 		items := strings.Split(element, ":") | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 		if len(items) != 2 { | 
					
						
							|  |  |  | 			// ignore empty lines | 
					
						
							| 
									
										
										
										
											2025-05-20 21:02:52 -07:00
										 |  |  | 			return | 
					
						
							| 
									
										
										
										
											2017-02-19 20:34:29 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-20 21:02:52 -07:00
										 |  |  | 	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) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-29 15:41:09 -04:00
										 |  |  | 	coreDnsImports := "" | 
					
						
							|  |  |  | 	thirdPartyImports := "" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2024-05-29 15:41:09 -04:00
										 |  |  | 		if strings.HasPrefix(v, githubOrg) { | 
					
						
							|  |  |  | 			coreDnsImports += `_ "` + v + `"` + "\n" | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			thirdPartyImports += `_ "` + v + `"` + "\n" | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-02-14 19:23:18 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-05-29 15:41:09 -04:00
										 |  |  | 	outs += coreDnsImports | 
					
						
							|  |  |  | 	if thirdPartyImports != "" { | 
					
						
							|  |  |  | 		outs += "\n" + thirdPartyImports | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-13 15:30:31 +08:00
										 |  |  | 	if err = os.WriteFile(file, res, 0644); err != nil { | 
					
						
							| 
									
										
										
										
											2017-10-16 10:49:18 +02:00
										 |  |  | 		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 ( | 
					
						
							| 
									
										
										
										
											2024-05-29 15:41:09 -04:00
										 |  |  | 	githubOrg    = "github.com/coredns" | 
					
						
							|  |  |  | 	pluginPath   = githubOrg + "/coredns/plugin/" | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	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
										 |  |  | ) |