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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 9 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
7 changes: 6 additions & 1 deletion tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}}"
8 changes: 8 additions & 0 deletions tmpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading