add support for SPDX license headers

This adds a new "-s" flag that will append an SPDX-License-Identifier
line to license headers.  If "-s=only" is specified, then only the SPDX
identifier will be used.

This also changes the "-l" flag to use SPDX identifiers, with mappings
to support the legacy "apache", "mit", and "mpl" values.  Together with
the "-s" flag, this allows SPDX headers for any arbitrary license type
to be added to files.

Co-authored-by: Bob Callaway <bcallawa@redhat.com>
This commit is contained in:
Will Norris
2021-07-26 20:26:45 -07:00
parent c2fdf83882
commit 97ae522f98
3 changed files with 113 additions and 19 deletions

38
tmpl.go
View File

@@ -25,25 +25,36 @@ import (
)
var licenseTemplate = map[string]string{
"apache": tmplApache,
"mit": tmplMIT,
"bsd": tmplBSD,
"mpl": tmplMPL,
"Apache-2.0": tmplApache,
"MIT": tmplMIT,
"bsd": tmplBSD,
"MPL-2.0": tmplMPL,
}
// maintain backwards compatibility by mapping legacy license types to their
// SPDX equivalents.
var legacyLicenseTypes = map[string]string{
"apache": "Apache-2.0",
"mit": "MIT",
"mpl": "MPL-2.0",
}
// licenseData specifies the data used to fill out a license template.
type licenseData struct {
Year string // Copyright year(s).
Holder string // Name of the copyright holder.
SPDXID string // SPDX Identifier
}
// fetchTemplate returns the license template for the specified license and
// optional templateFile. If templateFile is provided, the license is read
// from the specified file. Otherwise, a template is loaded for the specified
// license, if recognized.
func fetchTemplate(license string, templateFile string) (string, error) {
func fetchTemplate(license string, templateFile string, spdx spdxFlag) (string, error) {
var t string
if templateFile != "" {
if spdx == spdxOnly {
t = tmplSPDX
} else if templateFile != "" {
d, err := ioutil.ReadFile(templateFile)
if err != nil {
return "", fmt.Errorf("license file: %w", err)
@@ -53,7 +64,15 @@ func fetchTemplate(license string, templateFile string) (string, error) {
} else {
t = licenseTemplate[license]
if t == "" {
return "", fmt.Errorf("unknown license: %q", license)
if spdx == spdxOn {
// unknown license, but SPDX headers requested
t = tmplSPDX
} else {
return "", fmt.Errorf("unknown license: %q. Include the '-s' flag to request SPDX style headers using this license.", license)
}
} else if spdx == spdxOn {
// append spdx headers to recognized license
t = t + spdxSuffix
}
}
@@ -122,3 +141,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.`
const tmplMPL = `This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.`
const tmplSPDX = `{{ if and .Year .Holder }}Copyright {{.Year}} {{.Holder}}
{{ end }}SPDX-License-Identifier: {{.SPDXID}}`
const spdxSuffix = "\n\nSPDX-License-Identifier: {{.SPDXID}}"