/* Copyright 2019 The Kubernetes Authors All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package main import ( "bytes" "flag" "fmt" "io/ioutil" "log" "os" "path/filepath" "regexp" "strings" ) var ( skippedPaths = regexp.MustCompile(`Godeps|third_party|_gopath|_output|\.git|cluster/env.sh|vendor|test/e2e/generated/bindata.go|site/themes/docsy`) rootdir *string boilerplatedir *string ) func init() { cwd, _ := os.Getwd() boilerplatedir = flag.String("boilerplate-dir", cwd, "Boilerplate directory for boilerplate files") cwd += "/../../" rootdir = flag.String("rootdir", filepath.Dir(cwd), "Root directory to examine") } func main() { flag.Parse() refs := boilerplateRefs(*boilerplatedir) if len(refs) == 0 { log.Fatal("no references in ", *boilerplatedir) } files := filesToCheck(*rootdir, refs) for _, file := range files { if !filePasses(file, refs[fileExtension(file)]) { fmt.Println(file) } } } /* This function is to populate the refs variable with the different boilerplate/template for different extension. */ func boilerplateRefs(dir string) map[string][]byte { refs := make(map[string][]byte) files, _ := filepath.Glob(dir + "/*.txt") for _, filename := range files { re := regexp.MustCompile(`\.txt`) extension := strings.ToLower(fileExtension(re.ReplaceAllString(filename, ""))) data, err := ioutil.ReadFile(filename) if err != nil { log.Fatal(err) } re = regexp.MustCompile(`\r`) refs[extension] = re.ReplaceAll(data, nil) } return refs } /* Function to check whether the processed file is valid. Returning false means that the file does not the proper boilerplate template */ func filePasses(filename string, ref []byte) bool { var re *regexp.Regexp data, err := ioutil.ReadFile(filename) if err != nil { log.Fatal(err) } re = regexp.MustCompile(`\r`) data = re.ReplaceAll(data, nil) extension := fileExtension(filename) // remove build tags from the top of Go files if extension == "go" { re = regexp.MustCompile(`(?m)^(// \+build.*\n)+\n`) data = re.ReplaceAll(data, nil) } // remove shebang from the top of shell files if extension == "sh" { re = regexp.MustCompile(`(?m)^(#!.*\n)\n*`) data = re.ReplaceAll(data, nil) } // if our test file is smaller than the reference it surely fails! if len(data) < len(ref) { return false } data = data[:len(ref)] // Search for "Copyright YEAR" which exists in the boilerplate, but shouldn't in the real thing re = regexp.MustCompile(`Copyright YEAR`) if re.Match(data) { return false } // Replace all occurrences of the regex "Copyright \d{4}" with "Copyright YEAR" re = regexp.MustCompile(`Copyright \d{4}`) data = re.ReplaceAll(data, []byte(`Copyright YEAR`)) return bytes.Equal(data, ref) } /** Function to get the file extensin or the filename if the file has no extension */ func fileExtension(filename string) string { splitted := strings.Split(filepath.Base(filename), ".") return strings.ToLower(splitted[len(splitted)-1]) } /** Function to get all the files from the directory that heeds to be checked. */ func filesToCheck(rootDir string, extensions map[string][]byte) []string { var outFiles []string err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error { // remove current workdir from the beginig 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), "")) { if extensions[strings.ToLower(fileExtension(path))] != nil { outFiles = append(outFiles, path) } } return nil }) if err != nil { log.Fatal(err) } return outFiles }