|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes 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 | + "regexp" |
| 25 | + "strings" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + yearPlaceholder = "YEAR" |
| 30 | + boilerPlateStart = "Copyright " |
| 31 | + boilerPlateEnd = "limitations under the License." |
| 32 | +) |
| 33 | + |
| 34 | +var ( |
| 35 | + supportedExt = []string{".go", ".py", ".sh"} |
| 36 | + yearRegexp = regexp.MustCompile("(20)[0-9][0-9]") |
| 37 | + boilerPlate = []string{ |
| 38 | + boilerPlateStart + yearPlaceholder + " The Kubernetes Authors.", |
| 39 | + "", |
| 40 | + `Licensed under the Apache License, Version 2.0 (the "License");`, |
| 41 | + "you may not use this file except in compliance with the License.", |
| 42 | + "You may obtain a copy of the License at", |
| 43 | + "", |
| 44 | + " http://www.apache.org/licenses/LICENSE-2.0", |
| 45 | + "", |
| 46 | + "Unless required by applicable law or agreed to in writing, software", |
| 47 | + `distributed under the License is distributed on an "AS IS" BASIS,`, |
| 48 | + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", |
| 49 | + "See the License for the specific language governing permissions and", |
| 50 | + boilerPlateEnd, |
| 51 | + } |
| 52 | +) |
| 53 | + |
| 54 | +// trimLeadingComment strips a single line comment characters such as # or // |
| 55 | +// at the exact beginning of a line, but also the first possible space character after it. |
| 56 | +func trimLeadingComment(line, c string) string { |
| 57 | + if strings.Index(line, c) == 0 { |
| 58 | + x := len(c) |
| 59 | + if len(line) == x { |
| 60 | + return "" |
| 61 | + } |
| 62 | + if line[x] == byte(' ') { |
| 63 | + return line[x+1:] |
| 64 | + } |
| 65 | + return line[x:] |
| 66 | + } |
| 67 | + return line |
| 68 | +} |
| 69 | + |
| 70 | +// verifyFileExtension verifies if the file extensions is supported |
| 71 | +func isSupportedFileExtension(filePath string) bool { |
| 72 | + // check if the file has an extension |
| 73 | + idx := strings.LastIndex(filePath, ".") |
| 74 | + if idx == -1 { |
| 75 | + return false |
| 76 | + } |
| 77 | + |
| 78 | + // check if the file has a supported extension |
| 79 | + ext := filePath[idx : idx+len(filePath)-idx] |
| 80 | + for _, e := range supportedExt { |
| 81 | + if e == ext { |
| 82 | + return true |
| 83 | + } |
| 84 | + } |
| 85 | + return false |
| 86 | +} |
| 87 | + |
| 88 | +// verifyBoilerplate verifies if a string contains the boilerplate |
| 89 | +func verifyBoilerplate(contents string) error { |
| 90 | + idx := 0 |
| 91 | + foundBoilerplateStart := false |
| 92 | + lines := strings.Split(contents, "\n") |
| 93 | + for _, line := range lines { |
| 94 | + // handle leading comments |
| 95 | + line = trimLeadingComment(line, "//") |
| 96 | + line = trimLeadingComment(line, "#") |
| 97 | + |
| 98 | + // find the start of the boilerplate |
| 99 | + bpLine := boilerPlate[idx] |
| 100 | + if strings.Contains(line, boilerPlateStart) { |
| 101 | + foundBoilerplateStart = true |
| 102 | + |
| 103 | + // validate the year of the copyright |
| 104 | + yearWords := strings.Split(line, " ") |
| 105 | + expectedLen := len(strings.Split(boilerPlate[0], " ")) |
| 106 | + if len(yearWords) != expectedLen { |
| 107 | + return fmt.Errorf("copyright line should contain exactly %d words", expectedLen) |
| 108 | + } |
| 109 | + if !yearRegexp.MatchString(yearWords[1]) { |
| 110 | + return fmt.Errorf("cannot parse the year in the copyright line") |
| 111 | + } |
| 112 | + bpLine = strings.ReplaceAll(bpLine, yearPlaceholder, yearWords[1]) |
| 113 | + } |
| 114 | + |
| 115 | + // match line by line |
| 116 | + if foundBoilerplateStart { |
| 117 | + if line != bpLine { |
| 118 | + return fmt.Errorf("boilerplate line %d does not match\nexpected: %q\ngot: %q", idx+1, bpLine, line) |
| 119 | + } |
| 120 | + idx++ |
| 121 | + // exit after the last line is found |
| 122 | + if strings.Index(line, boilerPlateEnd) == 0 { |
| 123 | + break |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if !foundBoilerplateStart { |
| 129 | + return fmt.Errorf("the file is missing a boilerplate") |
| 130 | + } |
| 131 | + if idx < len(boilerPlate) { |
| 132 | + return errors.New("boilerplate has missing lines") |
| 133 | + } |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +// verifyFile verifies if a file contains the boilerplate |
| 138 | +func verifyFile(filePath string) error { |
| 139 | + if len(filePath) == 0 { |
| 140 | + return errors.New("empty file name") |
| 141 | + } |
| 142 | + |
| 143 | + if !isSupportedFileExtension(filePath) { |
| 144 | + fmt.Printf("skipping %q: unsupported file type\n", filePath) |
| 145 | + return nil |
| 146 | + } |
| 147 | + |
| 148 | + // read the file |
| 149 | + b, err := ioutil.ReadFile(filePath) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + return verifyBoilerplate(string(b)) |
| 155 | +} |
| 156 | + |
| 157 | +func main() { |
| 158 | + if len(os.Args) < 2 { |
| 159 | + fmt.Println("usage: " + |
| 160 | + "go run verify-boilerplate.go <path-to-file> <path-to-file> ...") |
| 161 | + os.Exit(1) |
| 162 | + } |
| 163 | + |
| 164 | + hasErr := false |
| 165 | + for _, filePath := range os.Args[1:] { |
| 166 | + if err := verifyFile(filePath); err != nil { |
| 167 | + fmt.Printf("error validating %q: %v\n", filePath, err) |
| 168 | + hasErr = true |
| 169 | + } |
| 170 | + } |
| 171 | + if hasErr { |
| 172 | + os.Exit(1) |
| 173 | + } |
| 174 | +} |
0 commit comments