minikube/hack/boilerplate/boilerplate.go

159 lines
4.2 KiB
Go
Raw Normal View History

/*
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"
"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-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-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)
}
files := filesToCheck(*rootdir, refs)
for _, file := range files {
if !filePasses(file, refs[fileExtension(file)]) {
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 {
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
}
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
*/
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)
}
2019-10-06 21:11:58 +00:00
/**
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])
}
2019-10-06 21:11:58 +00:00
/**
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 {
2019-10-08 07:36:14 +00:00
// 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
}