From 551f628197d7513ad6ca1c6a4358905d6c3e4d66 Mon Sep 17 00:00:00 2001 From: Thomas Roseman Date: Mon, 18 Aug 2025 21:30:38 +0000 Subject: [PATCH 1/3] Adding -s=strict for using only the SPDX notation. This preserves existing functionality by expanding valid values for the -s flag. --- README.md | 2 +- main.go | 17 +++++++++-------- tmpl.go | 7 ++++++- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c74ccbf..993b1c8 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ addlicense requires go 1.16 or later. -f license file -ignore file patterns to ignore, for example: -ignore **/*.go -ignore vendor/** -l license type: apache, bsd, mit, mpl (default "apache") - -s Include SPDX identifier in license header. Set -s=only to only include SPDX identifier. + -s Include SPDX identifier in license header. Set -s=only to only include SPDX identifier. Set -s=strict for SPDX license and copytright tags -v verbose mode: print the name of the files that are modified -y copyright year(s) (default is the current year) diff --git a/main.go b/main.go index 9e42e8a..02603bd 100644 --- a/main.go +++ b/main.go @@ -63,13 +63,13 @@ var ( ) 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(&spdx, "s", "Include SPDX identifier in license header. Set -s=only to only include SPDX identifier. Set -s=strict to only use SPDX tags.") flag.Usage = func() { fmt.Fprint(os.Stderr, helpText) flag.PrintDefaults() } - 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(&spdx, "s", "Include SPDX identifier in license header. Set -s=only to only include SPDX identifier.") } // stringSlice stores the results of a repeated command line flag as a string slice. @@ -88,9 +88,10 @@ func (i *stringSlice) Set(value string) error { type spdxFlag string const ( - spdxOff spdxFlag = "" - spdxOn spdxFlag = "true" // value set by flag package on bool flag - spdxOnly spdxFlag = "only" + spdxOff spdxFlag = "" + spdxOn spdxFlag = "true" // value set by flag package on bool flag + spdxOnly spdxFlag = "only" + spdxStrict spdxFlag = "strict" ) // IsBoolFlag causes a bare '-s' flag to be set as the string 'true'. This @@ -100,8 +101,8 @@ func (i *spdxFlag) String() string { return string(*i) } func (i *spdxFlag) Set(value string) error { v := spdxFlag(value) - if v != spdxOn && v != spdxOnly { - return fmt.Errorf("error: flag 's' expects '%v' or '%v'", spdxOn, spdxOnly) + if v != spdxOn && v != spdxStrict && v != spdxOnly { + return fmt.Errorf("error: flag 's' expects one of '%v' or '%v' or '%v'", spdxOn, spdxOnly, spdxStrict) } *i = v return nil diff --git a/tmpl.go b/tmpl.go index 08a2d08..afaac37 100644 --- a/tmpl.go +++ b/tmpl.go @@ -52,7 +52,9 @@ type licenseData struct { // license, if recognized. func fetchTemplate(license string, templateFile string, spdx spdxFlag) (string, error) { var t string - if spdx == spdxOnly { + if spdx == spdxStrict { + t = tmplSPDXStrict + } else if spdx == spdxOnly { t = tmplSPDX } else if templateFile != "" { d, err := ioutil.ReadFile(templateFile) @@ -146,4 +148,7 @@ file, You can obtain one at https://mozilla.org/MPL/2.0/.` const tmplSPDX = `{{ if .Holder }}Copyright{{ if .Year }} {{.Year}}{{ end }} {{.Holder}} {{ end }}SPDX-License-Identifier: {{.SPDXID}}` +const tmplSPDXStrict = `{{if .Holder}}SPDX-FileCopyrightText: {{ if .Year }} {{.Year}}{{ end}} {{.Holder}} +{{ end }}SPDX-License-Identifier: {{.SPDXID}}` + const spdxSuffix = "\n\nSPDX-License-Identifier: {{.SPDXID}}" From c7442da8846b679e7ac07635d4f898a8c6ecf2d6 Mon Sep 17 00:00:00 2001 From: Thomas Roseman Date: Mon, 18 Aug 2025 21:30:38 +0000 Subject: [PATCH 2/3] Adding -s=strict for using only the SPDX notation. This preserves existing functionality by expanding valid values for the -s flag. --- README.md | 2 +- main.go | 17 +++++++++-------- tmpl.go | 7 ++++++- tmpl_test.go | 8 ++++++++ 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c74ccbf..993b1c8 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ addlicense requires go 1.16 or later. -f license file -ignore file patterns to ignore, for example: -ignore **/*.go -ignore vendor/** -l license type: apache, bsd, mit, mpl (default "apache") - -s Include SPDX identifier in license header. Set -s=only to only include SPDX identifier. + -s Include SPDX identifier in license header. Set -s=only to only include SPDX identifier. Set -s=strict for SPDX license and copytright tags -v verbose mode: print the name of the files that are modified -y copyright year(s) (default is the current year) diff --git a/main.go b/main.go index 9e42e8a..02603bd 100644 --- a/main.go +++ b/main.go @@ -63,13 +63,13 @@ var ( ) 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(&spdx, "s", "Include SPDX identifier in license header. Set -s=only to only include SPDX identifier. Set -s=strict to only use SPDX tags.") flag.Usage = func() { fmt.Fprint(os.Stderr, helpText) flag.PrintDefaults() } - 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(&spdx, "s", "Include SPDX identifier in license header. Set -s=only to only include SPDX identifier.") } // stringSlice stores the results of a repeated command line flag as a string slice. @@ -88,9 +88,10 @@ func (i *stringSlice) Set(value string) error { type spdxFlag string const ( - spdxOff spdxFlag = "" - spdxOn spdxFlag = "true" // value set by flag package on bool flag - spdxOnly spdxFlag = "only" + spdxOff spdxFlag = "" + spdxOn spdxFlag = "true" // value set by flag package on bool flag + spdxOnly spdxFlag = "only" + spdxStrict spdxFlag = "strict" ) // IsBoolFlag causes a bare '-s' flag to be set as the string 'true'. This @@ -100,8 +101,8 @@ func (i *spdxFlag) String() string { return string(*i) } func (i *spdxFlag) Set(value string) error { v := spdxFlag(value) - if v != spdxOn && v != spdxOnly { - return fmt.Errorf("error: flag 's' expects '%v' or '%v'", spdxOn, spdxOnly) + if v != spdxOn && v != spdxStrict && v != spdxOnly { + return fmt.Errorf("error: flag 's' expects one of '%v' or '%v' or '%v'", spdxOn, spdxOnly, spdxStrict) } *i = v return nil diff --git a/tmpl.go b/tmpl.go index 08a2d08..afaac37 100644 --- a/tmpl.go +++ b/tmpl.go @@ -52,7 +52,9 @@ type licenseData struct { // license, if recognized. func fetchTemplate(license string, templateFile string, spdx spdxFlag) (string, error) { var t string - if spdx == spdxOnly { + if spdx == spdxStrict { + t = tmplSPDXStrict + } else if spdx == spdxOnly { t = tmplSPDX } else if templateFile != "" { d, err := ioutil.ReadFile(templateFile) @@ -146,4 +148,7 @@ file, You can obtain one at https://mozilla.org/MPL/2.0/.` const tmplSPDX = `{{ if .Holder }}Copyright{{ if .Year }} {{.Year}}{{ end }} {{.Holder}} {{ end }}SPDX-License-Identifier: {{.SPDXID}}` +const tmplSPDXStrict = `{{if .Holder}}SPDX-FileCopyrightText: {{ if .Year }} {{.Year}}{{ end}} {{.Holder}} +{{ end }}SPDX-License-Identifier: {{.SPDXID}}` + const spdxSuffix = "\n\nSPDX-License-Identifier: {{.SPDXID}}" diff --git a/tmpl_test.go b/tmpl_test.go index e263149..6ae81cf 100644 --- a/tmpl_test.go +++ b/tmpl_test.go @@ -124,6 +124,14 @@ func TestFetchTemplate(t *testing.T) { tmplSPDX, nil, }, + { + "apache license with SPDX strict", + "Apache-2.0", + "", + spdxStrict, + tmplSPDXStrict, + nil, + }, } for _, tt := range tests { From fbf20731ba4422d7c8a782e9dc2a86b496fe34e0 Mon Sep 17 00:00:00 2001 From: Thomas Roseman Date: Wed, 8 Oct 2025 21:06:08 +0000 Subject: [PATCH 3/3] Adding tests for spdx flag to confirm three flags operate as expected. --- main_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/main_test.go b/main_test.go index 5aa7648..5905498 100644 --- a/main_test.go +++ b/main_test.go @@ -555,3 +555,26 @@ func TestFileMatches(t *testing.T) { } } } + +func TestSPDXFlagSet(t *testing.T) { + spdxValue := "" + tests := []struct { + name string + i spdxFlag + spdxString string + wantErr bool + }{ + {"SPDX: On", spdxFlag(spdxValue), "true", false}, + {"SPDX: Only", spdxFlag(spdxValue), "only", false}, + {"SPDX: Strict", spdxFlag(spdxValue), "strict", false}, + {"SPDX: Off", spdxFlag(spdxValue), "", true}, + {"SPDX: Invalid", spdxFlag(spdxValue), "notvalid", true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.i.Set(tt.spdxString); (err != nil) != tt.wantErr { + t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}