mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
cmd: Move some license functions from helpers.go to licenses.go
This commit is contained in:
parent
28fe9aaa16
commit
78a8032244
2 changed files with 59 additions and 57 deletions
|
@ -21,9 +21,6 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// var BaseDir = ""
|
||||
|
@ -297,47 +294,6 @@ func safeWriteToDisk(inpath string, r io.Reader) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func getLicense() License {
|
||||
l := whichLicense()
|
||||
if l != "" {
|
||||
if x, ok := Licenses[l]; ok {
|
||||
return x
|
||||
}
|
||||
}
|
||||
|
||||
return Licenses["apache"]
|
||||
}
|
||||
|
||||
func whichLicense() string {
|
||||
// if explicitly flagged, use that
|
||||
if userLicense != "" {
|
||||
return matchLicense(userLicense)
|
||||
}
|
||||
|
||||
// if already present in the project, use that
|
||||
// TODO: Inspect project for existing license
|
||||
|
||||
// default to viper's setting
|
||||
|
||||
if viper.IsSet("license.header") || viper.IsSet("license.text") {
|
||||
if custom, ok := Licenses["custom"]; ok {
|
||||
custom.Header = viper.GetString("license.header")
|
||||
custom.Text = viper.GetString("license.text")
|
||||
Licenses["custom"] = custom
|
||||
return "custom"
|
||||
}
|
||||
}
|
||||
|
||||
return matchLicense(viper.GetString("license"))
|
||||
}
|
||||
|
||||
func copyrightLine() string {
|
||||
author := viper.GetString("author")
|
||||
year := time.Now().Format("2006")
|
||||
|
||||
return "Copyright © " + year + " " + author
|
||||
}
|
||||
|
||||
func commentifyString(in string) string {
|
||||
var newlines []string
|
||||
lines := strings.Split(in, "\n")
|
||||
|
|
|
@ -15,7 +15,12 @@
|
|||
|
||||
package cmd
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
//Licenses contains all possible licenses a user can chose from
|
||||
var Licenses map[string]License
|
||||
|
@ -31,18 +36,6 @@ type License struct {
|
|||
Header string // License header for source files
|
||||
}
|
||||
|
||||
// given a license name (in), try to match the license indicated
|
||||
func matchLicense(in string) string {
|
||||
for key, lic := range Licenses {
|
||||
for _, match := range lic.PossibleMatches {
|
||||
if strings.EqualFold(in, match) {
|
||||
return key
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
Licenses = make(map[string]License)
|
||||
|
||||
|
@ -73,3 +66,56 @@ func init() {
|
|||
// `,
|
||||
// }
|
||||
}
|
||||
|
||||
func getLicense() License {
|
||||
l := whichLicense()
|
||||
if l != "" {
|
||||
if x, ok := Licenses[l]; ok {
|
||||
return x
|
||||
}
|
||||
}
|
||||
|
||||
return Licenses["apache"]
|
||||
}
|
||||
|
||||
func whichLicense() string {
|
||||
// if explicitly flagged, use that
|
||||
if userLicense != "" {
|
||||
return matchLicense(userLicense)
|
||||
}
|
||||
|
||||
// if already present in the project, use that
|
||||
// TODO: Inspect project for existing license
|
||||
|
||||
// default to viper's setting
|
||||
|
||||
if viper.IsSet("license.header") || viper.IsSet("license.text") {
|
||||
if custom, ok := Licenses["custom"]; ok {
|
||||
custom.Header = viper.GetString("license.header")
|
||||
custom.Text = viper.GetString("license.text")
|
||||
Licenses["custom"] = custom
|
||||
return "custom"
|
||||
}
|
||||
}
|
||||
|
||||
return matchLicense(viper.GetString("license"))
|
||||
}
|
||||
|
||||
func copyrightLine() string {
|
||||
author := viper.GetString("author")
|
||||
year := time.Now().Format("2006")
|
||||
|
||||
return "Copyright © " + year + " " + author
|
||||
}
|
||||
|
||||
// given a license name (in), try to match the license indicated
|
||||
func matchLicense(in string) string {
|
||||
for key, lic := range Licenses {
|
||||
for _, match := range lic.PossibleMatches {
|
||||
if strings.EqualFold(in, match) {
|
||||
return key
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue