From bc2ae7b9b2342ed7df137e08dabb080277782ed0 Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Mon, 13 May 2024 18:29:10 +0300 Subject: [PATCH 01/11] add new extensions --- main.go | 12 ++++++++++-- tmpl.go | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index bee76c4..1cee4d6 100644 --- a/main.go +++ b/main.go @@ -276,7 +276,7 @@ func licenseHeader(path string, tmpl *template.Template, data licenseData) ([]by base := strings.ToLower(filepath.Base(path)) switch fileExtension(base) { - case ".c", ".h", ".gv", ".java", ".scala", ".kt", ".kts": + case ".c", ".h", ".gv", ".java", ".scala", ".kt", ".kts", ".qml": lic, err = executeTemplate(tmpl, data, "/*", " * ", " */") case ".js", ".mjs", ".cjs", ".jsx", ".tsx", ".css", ".scss", ".sass", ".ts": lic, err = executeTemplate(tmpl, data, "/**", " * ", " */") @@ -290,7 +290,7 @@ func licenseHeader(path string, tmpl *template.Template, data licenseData) ([]by lic, err = executeTemplate(tmpl, data, "", "% ", "") case ".hs", ".sql", ".sdl": lic, err = executeTemplate(tmpl, data, "", "-- ", "") - case ".html", ".xml", ".vue", ".wxi", ".wxl", ".wxs": + case ".html", ".xml", ".vue", ".wxi", ".wxl", ".wxs", ".ts": lic, err = executeTemplate(tmpl, data, "") case ".php": lic, err = executeTemplate(tmpl, data, "", "// ", "") @@ -298,11 +298,19 @@ func licenseHeader(path string, tmpl *template.Template, data licenseData) ([]by lic, err = executeTemplate(tmpl, data, "{#", "", "#}") case ".ml", ".mli", ".mll", ".mly": lic, err = executeTemplate(tmpl, data, "(**", " ", "*)") + case ".ts": + lic, err = executeTemplate(tmpl, data, "/**", " * ", " */") + if err { + lic, err = executeTemplate(tmpl, data, "") + } default: // handle various cmake files 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" { + lic, err = executeTemplate(tmpl, data, "", "// ", "") + } } return lic, err } diff --git a/tmpl.go b/tmpl.go index 83da7f2..7130ed4 100644 --- a/tmpl.go +++ b/tmpl.go @@ -89,7 +89,7 @@ func executeTemplate(t *template.Template, d licenseData, top, mid, bot string) return out.Bytes(), nil } -const tmplCommercial = `Copyright {{.Holder}} {{ if .Year }} 2023-{{.Year}}{{ end }} +const tmplCommercial = `Copyright {{.Holder}} {{ if .Year }} {{.Year}}{{ end }} You can not use the contents of the file in any way without AVROID, Ltd. written permission. From 1e0ac5342099490a45f9d833362c923c203c3e3e Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Mon, 13 May 2024 18:35:03 +0300 Subject: [PATCH 02/11] up --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 1cee4d6..ef6e639 100644 --- a/main.go +++ b/main.go @@ -278,7 +278,7 @@ func licenseHeader(path string, tmpl *template.Template, data licenseData) ([]by switch fileExtension(base) { case ".c", ".h", ".gv", ".java", ".scala", ".kt", ".kts", ".qml": lic, err = executeTemplate(tmpl, data, "/*", " * ", " */") - case ".js", ".mjs", ".cjs", ".jsx", ".tsx", ".css", ".scss", ".sass", ".ts": + case ".js", ".mjs", ".cjs", ".jsx", ".tsx", ".css", ".scss", ".sass": lic, err = executeTemplate(tmpl, data, "/**", " * ", " */") case ".cc", ".cpp", ".cs", ".go", ".hcl", ".hh", ".hpp", ".m", ".mm", ".proto", ".rs", ".swift", ".dart", ".groovy", ".v", ".sv": lic, err = executeTemplate(tmpl, data, "", "// ", "") @@ -290,7 +290,7 @@ func licenseHeader(path string, tmpl *template.Template, data licenseData) ([]by lic, err = executeTemplate(tmpl, data, "", "% ", "") case ".hs", ".sql", ".sdl": lic, err = executeTemplate(tmpl, data, "", "-- ", "") - case ".html", ".xml", ".vue", ".wxi", ".wxl", ".wxs", ".ts": + case ".html", ".xml", ".vue", ".wxi", ".wxl", ".wxs": lic, err = executeTemplate(tmpl, data, "") case ".php": lic, err = executeTemplate(tmpl, data, "", "// ", "") From 20f7a4078b9dcf7b21accf9b2ae8f16ebd8ca4d7 Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Mon, 13 May 2024 18:37:26 +0300 Subject: [PATCH 03/11] up --- Makefile | 2 +- main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index ed013f9..1c7dffa 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ APP=addlicense GROUP=devops REVISION="" -VERSION=1.1.1$(REVISION) +VERSION=1.1.2$(REVISION) DOCKER_REGISTRY=harbor.avroid.tech all: diff --git a/main.go b/main.go index ef6e639..9b0b447 100644 --- a/main.go +++ b/main.go @@ -290,7 +290,7 @@ func licenseHeader(path string, tmpl *template.Template, data licenseData) ([]by lic, err = executeTemplate(tmpl, data, "", "% ", "") case ".hs", ".sql", ".sdl": lic, err = executeTemplate(tmpl, data, "", "-- ", "") - case ".html", ".xml", ".vue", ".wxi", ".wxl", ".wxs": + case ".html", ".xml", ".vue", ".wxi", ".wxl", ".wxs", ".qrc": lic, err = executeTemplate(tmpl, data, "") case ".php": lic, err = executeTemplate(tmpl, data, "", "// ", "") From 97e873c729b8146b7c2a64f8f0ed877364d6cf40 Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Mon, 13 May 2024 18:54:01 +0300 Subject: [PATCH 04/11] up --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 9b0b447..aca9966 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,5 @@ // This program ensures source code files have copyright license headers. -// See usage with "addlicense -h". +// See usage with "addlicense -h". package main From b1009a7e715261c6ce883a4afbfb2ccedfca7aab Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Tue, 14 May 2024 12:27:26 +0300 Subject: [PATCH 05/11] up --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index aca9966..4824608 100644 --- a/main.go +++ b/main.go @@ -298,10 +298,10 @@ func licenseHeader(path string, tmpl *template.Template, data licenseData) ([]by lic, err = executeTemplate(tmpl, data, "{#", "", "#}") case ".ml", ".mli", ".mll", ".mly": lic, err = executeTemplate(tmpl, data, "(**", " ", "*)") - case ".ts": + case ".ts": lic, err = executeTemplate(tmpl, data, "/**", " * ", " */") - if err { - lic, err = executeTemplate(tmpl, data, "") + if err != nil { + lic, err = executeTemplate(tmpl, data, "") } default: // handle various cmake files From 33190ea6c357d70597d6da856b28bc061576ac40 Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Tue, 14 May 2024 12:29:00 +0300 Subject: [PATCH 06/11] up --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 4824608..e6c056d 100644 --- a/main.go +++ b/main.go @@ -300,9 +300,9 @@ func licenseHeader(path string, tmpl *template.Template, data licenseData) ([]by lic, err = executeTemplate(tmpl, data, "(**", " ", "*)") case ".ts": lic, err = executeTemplate(tmpl, data, "/**", " * ", " */") - if err != nil { + if err != nil { lic, err = executeTemplate(tmpl, data, "") - } + } default: // handle various cmake files if base == "cmakelists.txt" || strings.HasSuffix(base, ".cmake.in") || strings.HasSuffix(base, ".cmake") { From fa159a0c77f629ac0688179e10f8f7c95f48a929 Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Tue, 14 May 2024 12:31:47 +0300 Subject: [PATCH 07/11] up --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index e6c056d..756ffc4 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,5 @@ // This program ensures source code files have copyright license headers. -// See usage with "addlicense -h". +// See usage with "addlicense -h". package main From 973d52fa7cdd564b8b62ca185c6e620149ea7d3e Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Thu, 16 May 2024 18:57:04 +0300 Subject: [PATCH 08/11] up --- main.go | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 756ffc4..16aae53 100644 --- a/main.go +++ b/main.go @@ -37,9 +37,11 @@ Flags: ` var ( - skipExtensionFlags stringSlice - ignorePatterns stringSlice - spdx spdxFlag + skipExtensionFlags stringSlice + ignorePatterns stringSlice + filesFlags stringSlice + spdx spdxFlag + fileList []string holder = flag.String("c", "Avroid, Ltd.", "copyright holder") license = flag.String("l", "commercial", "license type: commercial, apache, bsd, mit, mpl") @@ -56,6 +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(&spdx, "s", "Include SPDX identifier in license header. Set -s=only to only include SPDX identifier.") } @@ -112,6 +115,14 @@ func main() { } } + // convert string of files to list of files + for _, l := range listFiles { + files := strings.Fields(l) + for _, file := range files { + fileList := append(fileList, file) + } + } + // map legacy license values if t, ok := legacyLicenseTypes[*license]; ok { *license = t @@ -160,6 +171,10 @@ func main() { fmt.Printf("%s\n", f.path) return errors.New("missing license header") } + if !hasCorrectAvroidLicense(f.path) { + fmt.Printf("%s\n", f.path) + return errors.New("incorrect year in license header") + } } else { modified, err := addLicense(f.path, f.mode, t, data) if err != nil { @@ -209,6 +224,9 @@ func walk(ch chan<- *file, start string) error { } return nil } + if fileList !fileMatches(path, fileList) { + return nil + } ch <- &file{path, fi.Mode()} return nil }) @@ -365,11 +383,23 @@ func isGenerated(b []byte) bool { } func hasLicense(b []byte) bool { - n := 1000 - if len(b) < 1000 { + n := 500 + if len(b) < 500 { n = len(b) } return bytes.Contains(bytes.ToLower(b[:n]), []byte("copyright")) || bytes.Contains(bytes.ToLower(b[:n]), []byte("mozilla public")) || bytes.Contains(bytes.ToLower(b[:n]), []byte("spdx-license-identifier")) } + +func hasCorrectAvroidLicense(b []byte) 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)) + } + + return nil +} From 73cc0f78f551d91e5dda471c83ba39774a570bac Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Fri, 17 May 2024 15:12:04 +0300 Subject: [PATCH 09/11] 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 } From 683554886b707168cbe2b1875a1561fdf968f662 Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Fri, 17 May 2024 16:49:45 +0300 Subject: [PATCH 10/11] up --- main.go | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 8af3acf..11381f5 100644 --- a/main.go +++ b/main.go @@ -39,9 +39,7 @@ Flags: var ( skipExtensionFlags stringSlice ignorePatterns stringSlice - filesFlags stringSlice spdx spdxFlag - fileList []string holder = flag.String("c", "Avroid, Ltd.", "copyright holder") license = flag.String("l", "commercial", "license type: commercial, apache, bsd, mit, mpl") @@ -58,7 +56,6 @@ 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(&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.") } @@ -115,14 +112,6 @@ func main() { } } - // convert string of files to list of files - for _, l := range fileList { - files := strings.Fields(l) - for _, file := range files { - fileList = append(fileList, file) - } - } - // map legacy license values if t, ok := legacyLicenseTypes[*license]; ok { *license = t @@ -173,11 +162,11 @@ func main() { return err } if !hasLicense { - fmt.Printf("%s\n", f.path) + fmt.Printf("%s: missing license header\n", f.path) return errors.New("missing license header") } if !hasCorrectAvroidLicense { - fmt.Printf("%s\n", f.path) + fmt.Printf("%s: incorrect year in license header\n", f.path) return errors.New("incorrect year in license header") } } else { @@ -229,9 +218,6 @@ func walk(ch chan<- *file, start string) error { } return nil } - if !fileMatches(path, fileList) { - return nil - } ch <- &file{path, fi.Mode()} return nil }) From 354e6707bb279c89cb24609d726d03b365655ba1 Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Fri, 17 May 2024 16:51:02 +0300 Subject: [PATCH 11/11] fix indents --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 11381f5..a6c2dd5 100644 --- a/main.go +++ b/main.go @@ -238,7 +238,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)