Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ addlicense requires go 1.16 or later.
-f custom license file (no default)
-l license type: apache, bsd, mit, mpl (defaults to "apache")
-y year (defaults to current year)
-x custom license check, can be used with custom license to properly check presence of header (defaults to "(c)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string "(c)" may result in some false positives, e.g. an (a) (b) (c) list or a reference to a 501(c)3 organization.

-check check only mode: verify presence of license headers and exit with non-zero code if missing
-ignore file patterns to ignore, for example: -ignore **/*.go -ignore vendor/**

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var (
license = flag.String("l", "apache", "license type: apache, bsd, mit, mpl")
licensef = flag.String("f", "", "license file")
year = flag.String("y", fmt.Sprint(time.Now().Year()), "copyright year(s)")
copyright = flag.String("x", "(c)", "custom copyright string used to determine license header presence")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By defining (c) as the default value of the flag, it gets added as a copyright header for anyone who doesn't specify the flag. Empty string is probably a better default, but don't do a Contains check on empty string, because all arrays contain a zero-byte subarray.

Is there a reason you chose x as the flag name? I can't think of a mnemonic. A full-word flag name is probably clearer.

verbose = flag.Bool("v", false, "verbose mode: print the name of the files that are modified")
checkonly = flag.Bool("check", false, "check only mode: verify presence of license headers and exit with non-zero code if missing")
)
Expand Down Expand Up @@ -371,5 +372,6 @@ func hasLicense(b []byte) bool {
}
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"))
bytes.Contains(bytes.ToLower(b[:n]), []byte("spdx-license-identifier")) ||
bytes.Contains(bytes.ToLower(b[:n]), []byte(*copyright))
}
27 changes: 27 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ func TestMultiyear(t *testing.T) {
run(t, "diff", samplefile, sampleLicensed)
}

func TestCustomCopyright(t *testing.T) {
if os.Getenv("RUNME") != "" {
main()
return
}

tmp := tempDir(t)
t.Logf("tmp dir: %s", tmp)
samplefile := filepath.Join(tmp, "file.c")
const sampleLicensed = "testdata/custom_copyright_file.c"
const customLicense = "testdata/custom_copyright.tpl"

run(t, "cp", "testdata/initial/file.c", samplefile)
cmd := exec.Command(os.Args[0],
"-test.run=TestCustomCopyright",
"-f", customLicense, "-c", "Google LLC", "-y", "2022",
"-x", "©", samplefile,
)
cmd.Env = []string{"RUNME=1"}
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("%v\n%s", err, out)
}
run(t, "diff", samplefile, sampleLicensed)
}

func TestWriteErrors(t *testing.T) {
if os.Getenv("RUNME") != "" {
main()
Expand Down Expand Up @@ -248,6 +273,7 @@ func TestAddLicense(t *testing.T) {
// ensure files with existing license or generated files are
// skipped. No need to test all permutations of these, since
// there are specific tests below.
{"// (c) 2022 Acme\ncontent", "// (c) 2022 Acme\ncontent", false},
{"// Copyright 2000 Acme\ncontent", "// Copyright 2000 Acme\ncontent", false},
{"// Code generated by go generate; DO NOT EDIT.\ncontent", "// Code generated by go generate; DO NOT EDIT.\ncontent", false},
}
Expand Down Expand Up @@ -395,6 +421,7 @@ func TestHasLicense(t *testing.T) {

{"Copyright 2000", true},
{"CoPyRiGhT 2000", true},
{"(c) 2022", true},
{"Subject to the terms of the Mozilla Public License", true},
{"SPDX-License-Identifier: MIT", true},
{"spdx-license-identifier: MIT", true},
Expand Down
3 changes: 3 additions & 0 deletions testdata/custom_copyright.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
© {{.Year}} {{.Holder}}

Custom License Template
12 changes: 12 additions & 0 deletions testdata/custom_copyright_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* © 2022 Google LLC
*
* Custom License Template
*/

#include <stdio.h>

int main() {
printf("Hello world\n");
return 0;
}