This commit is contained in:
aleksandr.vodyanov
2024-05-17 15:12:04 +03:00
parent 973d52fa7c
commit 73cc0f78f5

36
main.go
View File

@@ -58,7 +58,7 @@ func init() {
}
flag.Var(&skipExtensionFlags, "skip", "[deprecated: see -ignore] file extensions to skip, for example: -skip rb -skip go")
flag.Var(&ignorePatterns, "ignore", "file patterns to ignore, for example: -ignore **/*.go -ignore vendor/**")
flag.Var(&listFiles, "files", "list of files to include, for example: -files 'src/main.c src/header.h'")
flag.Var(&filesFlags, "files", "list of files to include, for example: -files 'src/main.c src/header.h'")
flag.Var(&spdx, "s", "Include SPDX identifier in license header. Set -s=only to only include SPDX identifier.")
}
@@ -116,10 +116,10 @@ func main() {
}
// convert string of files to list of files
for _, l := range listFiles {
for _, l := range fileList {
files := strings.Fields(l)
for _, file := range files {
fileList := append(fileList, file)
fileList = append(fileList, file)
}
}
@@ -167,11 +167,16 @@ func main() {
log.Printf("%s: %v", f.path, err)
return err
}
hasCorrectAvroidLicense, err := fileHasCorrectAvroidLicense(f.path, data)
if err != nil {
log.Printf("%s: %v", f.path, err)
return err
}
if !hasLicense {
fmt.Printf("%s\n", f.path)
return errors.New("missing license header")
}
if !hasCorrectAvroidLicense(f.path) {
if !hasCorrectAvroidLicense {
fmt.Printf("%s\n", f.path)
return errors.New("incorrect year in license header")
}
@@ -224,7 +229,7 @@ func walk(ch chan<- *file, start string) error {
}
return nil
}
if fileList !fileMatches(path, fileList) {
if !fileMatches(path, fileList) {
return nil
}
ch <- &file{path, fi.Mode()}
@@ -285,6 +290,17 @@ func fileHasLicense(path string) (bool, error) {
return hasLicense(b) || isGenerated(b), nil
}
// fileHasLicense reports whether the file at path contains a correct Avroid license header.
func fileHasCorrectAvroidLicense(path string, data licenseData) (bool, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return false, err
}
// If generated, we count it as if it has a license.
return hasCorrectAvroidLicense(b, data), nil
}
// licenseHeader populates the provided license template with data, and returns
// licenseHeader populates the provided license template with data, and returns
// it with the proper prefix for the file type specified by path. The file does
// not need to actually exist, only its name is used to determine the prefix.
@@ -326,7 +342,7 @@ func licenseHeader(path string, tmpl *template.Template, data licenseData) ([]by
if base == "cmakelists.txt" || strings.HasSuffix(base, ".cmake.in") || strings.HasSuffix(base, ".cmake") {
lic, err = executeTemplate(tmpl, data, "", "# ", "")
}
if base == "Jenkinsfile" || strings.HasSuffix(base, ".groovy" {
if base == "Jenkinsfile" || strings.HasSuffix(base, ".groovy") {
lic, err = executeTemplate(tmpl, data, "", "// ", "")
}
}
@@ -392,14 +408,14 @@ func hasLicense(b []byte) bool {
bytes.Contains(bytes.ToLower(b[:n]), []byte("spdx-license-identifier"))
}
func hasCorrectAvroidLicense(b []byte) bool {
func hasCorrectAvroidLicense(b []byte, data licenseData) bool {
n := 500
if len(b) < 500 {
n = len(b)
}
if bytes.Contains([]byte("AVROID, Ltd. written permission")) {
return bytes.Contains([]byte(fmt.Sprintf("Copyright (c) AVROID, Ltd., %s", data.Year))
if bytes.Contains(bytes.ToLower(b[:n]), []byte("avroid, ltd. written permission")) {
return bytes.Contains(bytes.ToLower(b[:n]), []byte(fmt.Sprintf("copyright (c) avroid, ltd., %s", data.Year)))
}
return nil
return true
}