| 
									
										
										
										
											2022-03-18 14:11:14 +00:00
										 |  |  | //go:build ignore | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // generates plugin/chaos/zowners.go. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-11-29 13:17:05 +00:00
										 |  |  | 	"bufio" | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"log" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2019-11-29 13:17:05 +00:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() { | 
					
						
							|  |  |  | 	// top-level OWNERS file | 
					
						
							| 
									
										
										
										
											2019-11-29 13:17:05 +00:00
										 |  |  | 	o, err := owners("CODEOWNERS") | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	golist := `package chaos | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Owners are all GitHub handlers of all maintainers. | 
					
						
							|  |  |  | var Owners = []string{` | 
					
						
							|  |  |  | 	c := ", " | 
					
						
							| 
									
										
										
										
											2019-11-29 13:17:05 +00:00
										 |  |  | 	for i, a := range o { | 
					
						
							|  |  |  | 		if i == len(o)-1 { | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | 			c = "}" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		golist += fmt.Sprintf("%q%s", a, c) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-07-04 14:50:49 +08:00
										 |  |  | 	// to prevent `No newline at end of file` with gofmt | 
					
						
							|  |  |  | 	golist += "\n" | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-13 15:30:31 +08:00
										 |  |  | 	if err := os.WriteFile("plugin/chaos/zowners.go", []byte(golist), 0644); err != nil { | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | 		log.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-29 13:17:05 +00:00
										 |  |  | func owners(path string) ([]string, error) { | 
					
						
							|  |  |  | 	// simple line, by line based format | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// # In this example, @doctocat owns any files in the build/logs | 
					
						
							|  |  |  | 	// # directory at the root of the repository and any of its | 
					
						
							|  |  |  | 	// # subdirectories. | 
					
						
							|  |  |  | 	// /build/logs/ @doctocat | 
					
						
							|  |  |  | 	f, err := os.Open(path) | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-29 13:17:05 +00:00
										 |  |  | 	scanner := bufio.NewScanner(f) | 
					
						
							|  |  |  | 	users := map[string]struct{}{} | 
					
						
							|  |  |  | 	for scanner.Scan() { | 
					
						
							|  |  |  | 		text := scanner.Text() | 
					
						
							|  |  |  | 		if len(text) == 0 { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if text[0] == '#' { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ele := strings.Fields(text) | 
					
						
							|  |  |  | 		if len(ele) == 0 { | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-11-29 13:17:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// ok ele[0] is the path, the rest are (in our case) github usernames prefixed with @ | 
					
						
							|  |  |  | 		for _, s := range ele[1:] { | 
					
						
							|  |  |  | 			if len(s) <= 1 { | 
					
						
							|  |  |  | 				continue | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-11-29 13:17:05 +00:00
										 |  |  | 			users[s[1:]] = struct{}{} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := scanner.Err(); err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	u := []string{} | 
					
						
							|  |  |  | 	for k := range users { | 
					
						
							|  |  |  | 		if strings.HasPrefix(k, "@") { | 
					
						
							|  |  |  | 			k = k[1:] | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-11-29 13:17:05 +00:00
										 |  |  | 		u = append(u, k) | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-29 13:17:05 +00:00
										 |  |  | 	sort.Strings(u) | 
					
						
							|  |  |  | 	return u, nil | 
					
						
							| 
									
										
										
										
											2019-03-31 19:01:11 +01:00
										 |  |  | } |