Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update boilerplate.go #18637

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 13 additions & 17 deletions hack/boilerplate/boilerplate.go
Expand Up @@ -28,9 +28,9 @@ import (
)

var (
boilerplatedir = flag.String("boilerplate-dir", ".", "Boilerplate directory for boilerplate files")
rootdir = flag.String("rootdir", "../../", "Root directory to examine")
verbose = flag.Bool("v", false, "Verbose")
boilerplateDir = flag.String("boilerplate-dir", ".", "Directory containing boilerplate files")
rootDir = flag.String("root-dir", "../../", "Root directory to examine")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't change flags, without also changing the invocation. Preferably, the old name should be kept (and hidden) for backwards compatibility. This includes keeping the short option, when adding a new long option.

verbose = flag.Bool("verbose", false, "Verbose mode")
skippedPaths = regexp.MustCompile(`Godeps|third_party|_gopath|_output|\.git|cluster/env.sh|vendor|test/e2e/generated/bindata.go|site/themes/docsy|test/integration/testdata`)
windowdNewLine = regexp.MustCompile(`\r`)
txtExtension = regexp.MustCompile(`\.txt`)
Expand All @@ -41,17 +41,21 @@ var (
)

func main() {
refs, err := extensionToBoilerplate(*boilerplatedir)
flag.Parse()

refs, err := extensionToBoilerplate(*boilerplateDir)
if err != nil {
log.Fatal(err)
}
if len(refs) == 0 {
log.Fatal("no references in ", *boilerplatedir)
log.Fatal("no references found in ", *boilerplateDir)
}
files, err := filesToCheck(*rootdir, refs)

files, err := filesToCheck(*rootDir, refs)
if err != nil {
log.Fatal(err)
}

for _, file := range files {
pass, err := filePasses(file, refs[filepath.Ext(file)])
if err != nil {
Expand All @@ -65,13 +69,12 @@ func main() {
fmt.Println(path)
}
}

}

// extensionToBoilerplate returns a map of file extension to required boilerplate text.
func extensionToBoilerplate(dir string) (map[string][]byte, error) {
refs := make(map[string][]byte)
files, _ := filepath.Glob(dir + "/*.txt")

for _, filename := range files {
extension := strings.ToLower(filepath.Ext(txtExtension.ReplaceAllString(filename, "")))
data, err := os.ReadFile(filename)
Expand All @@ -80,6 +83,7 @@ func extensionToBoilerplate(dir string) (map[string][]byte, error) {
}
refs[extension] = windowdNewLine.ReplaceAll(data, nil)
}

if *verbose {
dir, err := filepath.Abs(dir)
if err != nil {
Expand All @@ -91,10 +95,10 @@ func extensionToBoilerplate(dir string) (map[string][]byte, error) {
}
fmt.Println()
}

return refs, nil
}

// filePasses checks whether the processed file is valid. Returning false means that the file does not the proper boilerplate template.
func filePasses(filename string, expectedBoilerplate []byte) (bool, error) {
data, err := os.ReadFile(filename)
if err != nil {
Expand All @@ -104,41 +108,33 @@ func filePasses(filename string, expectedBoilerplate []byte) (bool, error) {

extension := filepath.Ext(filename)

// remove build tags from the top of Go files
if extension == ".go" {
data = goBuildTag.ReplaceAll(data, nil)
}

// remove shebang from the top of shell files
if extension == ".sh" {
data = shebang.ReplaceAll(data, nil)
}

// if our test file is smaller than the reference it surely fails!
if len(data) < len(expectedBoilerplate) {
return false, nil
}

data = data[:len(expectedBoilerplate)]

// Search for "Copyright YEAR" which exists in the boilerplate, but shouldn't in the real thing
if copyright.Match(data) {
return false, nil
}

// Replace all occurrences of the regex "Copyright \d{4}" with "Copyright YEAR"
data = copyrightReal.ReplaceAll(data, []byte(`Copyright YEAR`))

return bytes.Equal(data, expectedBoilerplate), nil
}

// filesToCheck returns the list of the filers that will be checked for the boilerplate.
func filesToCheck(rootDir string, extensions map[string][]byte) ([]string, error) {
var outFiles []string
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, _ error) error {
// remove current workdir from the beginning of the path in case it matches the skipped path
cwd, _ := os.Getwd()
// replace "\" with "\\" for windows style path
re := regexp.MustCompile(`\\`)
re = regexp.MustCompile(`^` + re.ReplaceAllString(cwd, `\\`))
if !info.IsDir() && !skippedPaths.MatchString(re.ReplaceAllString(filepath.Dir(path), "")) {
Expand Down