Compare commits
10 Commits
b1009a7e71
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c4e9aca130 | ||
|
|
9e5a53975f | ||
|
|
58108532f9 | ||
|
|
346c3a9fa5 | ||
|
|
354e6707bb | ||
|
|
683554886b | ||
|
|
73cc0f78f5 | ||
|
|
973d52fa7c | ||
|
|
fa159a0c77 | ||
|
|
33190ea6c3 |
45
main.go
45
main.go
@@ -156,10 +156,19 @@ 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)
|
||||
fmt.Printf("%s: missing license header\n", f.path)
|
||||
return errors.New("missing license header")
|
||||
}
|
||||
if !hasCorrectAvroidLicense {
|
||||
fmt.Printf("%s: incorrect year in license header\n", f.path)
|
||||
return errors.New("incorrect year in license header")
|
||||
}
|
||||
} else {
|
||||
modified, err := addLicense(f.path, f.mode, t, data)
|
||||
if err != nil {
|
||||
@@ -267,6 +276,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.
|
||||
@@ -282,7 +302,7 @@ func licenseHeader(path string, tmpl *template.Template, data licenseData) ([]by
|
||||
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, "", "// ", "")
|
||||
case ".py", ".sh", ".yaml", ".yml", ".dockerfile", "dockerfile", ".rb", "gemfile", ".tcl", ".tf", ".bzl", ".pl", ".pp", "build", ".build", ".toml":
|
||||
case ".py", ".sh", ".yaml", ".yml", ".dockerfile", ".rb", "gemfile", ".tcl", ".tf", ".bzl", ".pl", ".pp", "build", ".build", ".toml":
|
||||
lic, err = executeTemplate(tmpl, data, "", "# ", "")
|
||||
case ".el", ".lisp":
|
||||
lic, err = executeTemplate(tmpl, data, "", ";; ", "")
|
||||
@@ -308,7 +328,10 @@ 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, "", "// ", "")
|
||||
}
|
||||
if base == "dockerfile" || strings.HasSuffix(base, ".dockerfile") {
|
||||
lic, err = executeTemplate(tmpl, data, "", "// ", "")
|
||||
}
|
||||
}
|
||||
@@ -365,11 +388,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, data licenseData) bool {
|
||||
n := 500
|
||||
if len(b) < 500 {
|
||||
n = len(b)
|
||||
}
|
||||
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 true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user