2019-10-05 17:31:39 +00:00
|
|
|
/*
|
|
|
|
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"
|
2019-10-06 20:56:53 +00:00
|
|
|
"flag"
|
2019-10-05 17:31:39 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-10-06 20:56:53 +00:00
|
|
|
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
|
|
|
|
)
|
2019-10-05 17:31:39 +00:00
|
|
|
|
2019-10-06 20:56:53 +00:00
|
|
|
func init() {
|
|
|
|
cwd, _ := os.Getwd()
|
|
|
|
boilerplatedir = flag.String("boilerplate-dir", cwd, "Boilerplate directory for boilerplate files")
|
2019-10-08 07:36:14 +00:00
|
|
|
cwd += "/../../"
|
2019-10-06 20:56:53 +00:00
|
|
|
rootdir = flag.String("rootdir", filepath.Dir(cwd), "Root directory to examine")
|
|
|
|
}
|
2019-10-05 17:31:39 +00:00
|
|
|
|
2019-10-06 20:56:53 +00:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2019-10-08 07:28:17 +00:00
|
|
|
refs := boilerplateRefs(*boilerplatedir)
|
2019-10-06 20:56:53 +00:00
|
|
|
if len(refs) == 0 {
|
|
|
|
log.Fatal("no references in ", *boilerplatedir)
|
2019-10-05 17:31:39 +00:00
|
|
|
}
|
2019-10-07 14:48:37 +00:00
|
|
|
files := filesToCheck(*rootdir, refs)
|
2019-10-05 17:31:39 +00:00
|
|
|
for _, file := range files {
|
2019-10-07 14:48:37 +00:00
|
|
|
if !filePasses(file, refs[fileExtension(file)]) {
|
2019-10-05 17:31:39 +00:00
|
|
|
fmt.Println(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-06 21:11:58 +00:00
|
|
|
/*
|
|
|
|
This function is to populate the refs variable with the
|
|
|
|
different boilerplate/template for different extension.
|
|
|
|
*/
|
2019-10-08 07:28:17 +00:00
|
|
|
func boilerplateRefs(dir string) map[string][]byte {
|
2019-10-05 17:31:39 +00:00
|
|
|
refs := make(map[string][]byte)
|
|
|
|
files, _ := filepath.Glob(dir + "/*.txt")
|
|
|
|
for _, filename := range files {
|
2019-10-07 14:48:37 +00:00
|
|
|
re := regexp.MustCompile(`\.txt`)
|
|
|
|
extension := strings.ToLower(fileExtension(re.ReplaceAllString(filename, "")))
|
2019-10-05 17:31:39 +00:00
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-07 14:48:37 +00:00
|
|
|
re = regexp.MustCompile(`\r`)
|
2019-10-05 17:31:39 +00:00
|
|
|
refs[extension] = re.ReplaceAll(data, nil)
|
|
|
|
}
|
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
2019-10-06 21:11:58 +00:00
|
|
|
/*
|
|
|
|
Function to check whether the processed file
|
|
|
|
is valid.
|
|
|
|
Returning false means that the file does not the
|
|
|
|
proper boilerplate template
|
|
|
|
*/
|
2019-10-05 17:31:39 +00:00
|
|
|
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)
|
|
|
|
|
2019-10-07 14:48:37 +00:00
|
|
|
extension := fileExtension(filename)
|
2019-10-05 17:31:39 +00:00
|
|
|
|
|
|
|
// remove build tags from the top of Go files
|
|
|
|
if extension == "go" {
|
2019-10-06 21:06:41 +00:00
|
|
|
re = regexp.MustCompile(`(?m)^(// \+build.*\n)+\n`)
|
2019-10-05 17:31:39 +00:00
|
|
|
data = re.ReplaceAll(data, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove shebang from the top of shell files
|
|
|
|
if extension == "sh" {
|
2019-10-06 21:06:41 +00:00
|
|
|
re = regexp.MustCompile(`(?m)^(#!.*\n)\n*`)
|
2019-10-05 17:31:39 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-10-06 21:11:58 +00:00
|
|
|
/**
|
|
|
|
Function to get the file extensin or the filename if the file has no extension
|
|
|
|
*/
|
2019-10-07 14:48:37 +00:00
|
|
|
func fileExtension(filename string) string {
|
2019-10-05 17:31:39 +00:00
|
|
|
splitted := strings.Split(filepath.Base(filename), ".")
|
|
|
|
return strings.ToLower(splitted[len(splitted)-1])
|
|
|
|
}
|
|
|
|
|
2019-10-06 21:11:58 +00:00
|
|
|
/**
|
|
|
|
Function to get all the files from the directory that heeds to be checked.
|
|
|
|
*/
|
2019-10-07 14:48:37 +00:00
|
|
|
func filesToCheck(rootDir string, extensions map[string][]byte) []string {
|
2019-10-05 17:31:39 +00:00
|
|
|
var outFiles []string
|
2019-10-06 21:06:41 +00:00
|
|
|
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
|
2019-10-08 07:36:14 +00:00
|
|
|
// remove current workdir from the beginig of the path in case it matches the skipped path
|
2019-10-07 14:48:37 +00:00
|
|
|
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 {
|
2019-10-06 21:06:41 +00:00
|
|
|
outFiles = append(outFiles, path)
|
2019-10-05 17:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-06 21:06:41 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2019-10-05 17:31:39 +00:00
|
|
|
}
|
|
|
|
return outFiles
|
|
|
|
}
|