|
| 1 | +/* |
| 2 | + * Copyright The Dragonfly Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "strings" |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + start = " * Copyright " |
| 29 | + end = " * limitations under the License." |
| 30 | + boilerplate = []string{ |
| 31 | + ` * Copyright The Dragonfly Authors.`, |
| 32 | + ` *`, |
| 33 | + ` * Licensed under the Apache License, Version 2.0 (the "License");`, |
| 34 | + ` * you may not use this file except in compliance with the License.`, |
| 35 | + ` * You may obtain a copy of the License at`, |
| 36 | + ` *`, |
| 37 | + ` * http://www.apache.org/licenses/LICENSE-2.0`, |
| 38 | + ` *`, |
| 39 | + ` * Unless required by applicable law or agreed to in writing, software`, |
| 40 | + ` * distributed under the License is distributed on an "AS IS" BASIS,`, |
| 41 | + ` * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.`, |
| 42 | + ` * See the License for the specific language governing permissions and`, |
| 43 | + ` * limitations under the License.`, |
| 44 | + } |
| 45 | +) |
| 46 | + |
| 47 | +// checkBoilerplate checks if the input string contains the boilerplate |
| 48 | +func checkBoilerplate(content string) error { |
| 49 | + // ignore generated files |
| 50 | + if strings.Contains(content, "DO NOT EDIT") { |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + index := 0 |
| 55 | + foundStart := false |
| 56 | + lines := strings.Split(content, "\n") |
| 57 | + for _, line := range lines { |
| 58 | + // find the start of the boilerplate |
| 59 | + bpLine := boilerplate[index] |
| 60 | + if strings.Contains(line, start) { |
| 61 | + foundStart = true |
| 62 | + } |
| 63 | + |
| 64 | + // match line by line |
| 65 | + if foundStart { |
| 66 | + if line != bpLine { |
| 67 | + return fmt.Errorf("boilerplate line %d does not match\nexpected: %q\ngot: %q", index+1, bpLine, line) |
| 68 | + } |
| 69 | + index++ |
| 70 | + // exit after the last line is found |
| 71 | + if strings.Index(line, end) == 0 { |
| 72 | + break |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if !foundStart { |
| 78 | + return fmt.Errorf("the file is missing a boilerplate") |
| 79 | + } |
| 80 | + if index < len(boilerplate) { |
| 81 | + return errors.New("boilerplate has missing lines") |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// verifyFile verifies if a file contains the boilerplate |
| 87 | +func verifyFile(filePath string) error { |
| 88 | + if len(filePath) == 0 { |
| 89 | + return errors.New("empty file name") |
| 90 | + } |
| 91 | + |
| 92 | + // check file extension is go |
| 93 | + idx := strings.LastIndex(filePath, ".") |
| 94 | + if idx == -1 { |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + // check if the file has a supported extension |
| 99 | + ext := filePath[idx : idx+len(filePath)-idx] |
| 100 | + if ext != ".go" { |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + // read the file |
| 105 | + b, err := ioutil.ReadFile(filePath) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + return checkBoilerplate(string(b)) |
| 111 | +} |
| 112 | + |
| 113 | +func main() { |
| 114 | + if len(os.Args) < 2 { |
| 115 | + fmt.Println("usage: go run check-boilerplate.go <path-to-file> <path-to-file> ...") |
| 116 | + os.Exit(1) |
| 117 | + } |
| 118 | + |
| 119 | + hasErr := false |
| 120 | + for _, filePath := range os.Args[1:] { |
| 121 | + if err := verifyFile(filePath); err != nil { |
| 122 | + fmt.Printf("error validating %q: %v\n", filePath, err) |
| 123 | + hasErr = true |
| 124 | + } |
| 125 | + } |
| 126 | + if hasErr { |
| 127 | + os.Exit(1) |
| 128 | + } |
| 129 | +} |
0 commit comments