Improve Cobra Generator’s licensing helper

- Adopt standardized short identifiers from the SPDX License List,
   see http://spdx.org/licenses/
 - Add SPDX-License-Identifier in the source header, see ESR’s
   endorsement in http://esr.ibiblio.org/?p=6867
 - Print a message telling the user which license is actually chosen

TODO: Work-in-progress, not ready for commit.  Comments welcome!
This commit is contained in:
Anthony Fok 2015-11-20 16:42:37 -07:00
parent 4b096512f3
commit b0ace35ce7
4 changed files with 26 additions and 14 deletions

View file

@ -68,6 +68,7 @@ func createCmdFile(cmdName string) {
lic := getLicense() lic := getLicense()
template := `{{ comment .copyright }} template := `{{ comment .copyright }}
// SPDX-License-Identifier: {{ .licensename }}
{{ comment .license }} {{ comment .license }}
package cmd package cmd

View file

@ -301,11 +301,13 @@ func getLicense() License {
l := whichLicense() l := whichLicense()
if l != "" { if l != "" {
if x, ok := Licenses[l]; ok { if x, ok := Licenses[l]; ok {
fmt.Printf("Info: License matched, using %q\n", x.Identifier)
return x return x
} }
} }
return Licenses["apache"] fmt.Println("Info: License not specified or not found, using \"Apache-2.0\"")
return Licenses["Apache-2.0"]
} }
func whichLicense() string { func whichLicense() string {

View file

@ -108,6 +108,7 @@ func createMainFile() {
lic := getLicense() lic := getLicense()
template := `{{ comment .copyright }} template := `{{ comment .copyright }}
// SPDX-License-Identifier: {{ .licensename }}
{{ comment .license }} {{ comment .license }}
package main package main
@ -123,6 +124,7 @@ func main() {
data["copyright"] = copyrightLine() data["copyright"] = copyrightLine()
data["license"] = lic.Header data["license"] = lic.Header
data["licensename"] = lic.Identifier
data["importpath"] = guessImportPath() + "/" + guessCmdDir() data["importpath"] = guessImportPath() + "/" + guessCmdDir()
err := writeTemplateToFile(ProjectPath(), "main.go", template, data) err := writeTemplateToFile(ProjectPath(), "main.go", template, data)
@ -136,6 +138,7 @@ func createRootCmdFile() {
lic := getLicense() lic := getLicense()
template := `{{ comment .copyright }} template := `{{ comment .copyright }}
// SPDX-License-Identifier: {{ .licensename }}
{{ comment .license }} {{ comment .license }}
package cmd package cmd

View file

@ -1,4 +1,5 @@
// Copyright © 2015 Steve Francia <spf@spf13.com>. // Copyright © 2015 Steve Francia <spf@spf13.com>.
// SPDX-License-Identifier: Apache-2.0
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -20,7 +21,8 @@ import "strings"
var Licenses map[string]License var Licenses map[string]License
type License struct { type License struct {
Name string // The type of license in use Identifier string // SPDX License Identifier
Name string // The long name of the license in use
PossibleMatches []string // Similar names to guess PossibleMatches []string // Similar names to guess
Text string // License text data Text string // License text data
Header string // License header for source files Header string // License header for source files
@ -41,9 +43,10 @@ func matchLicense(in string) string {
func init() { func init() {
Licenses = make(map[string]License) Licenses = make(map[string]License)
Licenses["apache"] = License{ Licenses["Apache-2.0"] = License{
Name: "Apache 2.0", Identifier: "Apache-2.0",
PossibleMatches: []string{"apache", "apache20", "apache 2.0", "apache2.0", "apache-2.0"}, Name: "Apache License 2.0",
PossibleMatches: []string{"Apache-2.0", "Apache", "Apache-2", "apache20", "Apache 2.0", "Apache2.0"},
Header: ` Header: `
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -261,9 +264,10 @@ func init() {
`, `,
} }
Licenses["mit"] = License{ Licenses["MIT"] = License{
Name: "Mit", Identifier: "MIT",
PossibleMatches: []string{"mit"}, Name: "MIT License",
PossibleMatches: []string{"MIT", "Expat"},
Header: ` Header: `
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -306,9 +310,10 @@ THE SOFTWARE.
`, `,
} }
Licenses["bsd"] = License{ Licenses["BSD-3-Clause"] = License{
Name: "NewBSD", Identifier: "BSD-3-Clause",
PossibleMatches: []string{"bsd", "newbsd", "3 clause bsd"}, Name: `BSD 3-clause "New" or "Revised" License`,
PossibleMatches: []string{"BSD-3-Clause", "bsd", "newbsd", "3 clause bsd"},
Header: ` Header: `
All rights reserved. All rights reserved.
@ -367,9 +372,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
`, `,
} }
Licenses["freebsd"] = License{ Licenses["BSD-2-Clause"] = License{
Name: "Simplified BSD License", Identifier: "BSD-2-Clause",
PossibleMatches: []string{"freebsd", "simpbsd", "simple bsd", "2 clause bsd"}, Name: `BSD 2-clause "Simplified" License`,
PossibleMatches: []string{"BSD-2-Clause", "freebsd", "simpbsd", "simple bsd", "2 clause bsd"},
Header: ` Header: `
All rights reserved. All rights reserved.