From 73cc0f78f551d91e5dda471c83ba39774a570bac Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Fri, 17 May 2024 15:12:04 +0300 Subject: [PATCH] up --- main.go | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 16aae53..8af3acf 100644 --- a/main.go +++ b/main.go @@ -39,9 +39,9 @@ Flags: var ( skipExtensionFlags stringSlice ignorePatterns stringSlice - filesFlags stringSlice + filesFlags stringSlice spdx spdxFlag - fileList []string + fileList []string holder = flag.String("c", "Avroid, Ltd.", "copyright holder") license = flag.String("l", "commercial", "license type: commercial, apache, bsd, mit, mpl") @@ -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()} @@ -247,7 +252,7 @@ func fileMatches(path string, patterns []string) bool { // addLicense add a license to the file if missing. // // It returns true if the file was updated. -func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data licenseData) (bool, error) { +func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data licenseData) (bool, error) { var lic []byte var err error lic, err = licenseHeader(path, tmpl, data) @@ -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 }