This commit is contained in:
aleksandr.vodyanov
2024-05-16 18:57:04 +03:00
parent fa159a0c77
commit 973d52fa7c

34
main.go
View File

@@ -39,7 +39,9 @@ Flags:
var ( var (
skipExtensionFlags stringSlice skipExtensionFlags stringSlice
ignorePatterns stringSlice ignorePatterns stringSlice
filesFlags stringSlice
spdx spdxFlag spdx spdxFlag
fileList []string
holder = flag.String("c", "Avroid, Ltd.", "copyright holder") holder = flag.String("c", "Avroid, Ltd.", "copyright holder")
license = flag.String("l", "commercial", "license type: commercial, apache, bsd, mit, mpl") 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(&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(&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.") 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 // map legacy license values
if t, ok := legacyLicenseTypes[*license]; ok { if t, ok := legacyLicenseTypes[*license]; ok {
*license = t *license = t
@@ -160,6 +171,10 @@ func main() {
fmt.Printf("%s\n", f.path) fmt.Printf("%s\n", f.path)
return errors.New("missing license header") 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 { } else {
modified, err := addLicense(f.path, f.mode, t, data) modified, err := addLicense(f.path, f.mode, t, data)
if err != nil { if err != nil {
@@ -209,6 +224,9 @@ func walk(ch chan<- *file, start string) error {
} }
return nil return nil
} }
if fileList !fileMatches(path, fileList) {
return nil
}
ch <- &file{path, fi.Mode()} ch <- &file{path, fi.Mode()}
return nil return nil
}) })
@@ -365,11 +383,23 @@ func isGenerated(b []byte) bool {
} }
func hasLicense(b []byte) bool { func hasLicense(b []byte) bool {
n := 1000 n := 500
if len(b) < 1000 { if len(b) < 500 {
n = len(b) n = len(b)
} }
return bytes.Contains(bytes.ToLower(b[:n]), []byte("copyright")) || 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("mozilla public")) ||
bytes.Contains(bytes.ToLower(b[:n]), []byte("spdx-license-identifier")) 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
}