cmd: Move some license functions from helpers.go to licenses.go

This commit is contained in:
Albert Nigmatzianov 2017-04-20 20:28:16 +02:00
parent 28fe9aaa16
commit 78a8032244
2 changed files with 59 additions and 57 deletions

View File

@ -21,9 +21,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"text/template" "text/template"
"time"
"github.com/spf13/viper"
) )
// var BaseDir = "" // var BaseDir = ""
@ -297,47 +294,6 @@ func safeWriteToDisk(inpath string, r io.Reader) (err error) {
return 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 { func commentifyString(in string) string {
var newlines []string var newlines []string
lines := strings.Split(in, "\n") lines := strings.Split(in, "\n")

View File

@ -15,7 +15,12 @@
package cmd package cmd
import "strings" import (
"strings"
"time"
"github.com/spf13/viper"
)
//Licenses contains all possible licenses a user can chose from //Licenses contains all possible licenses a user can chose from
var Licenses map[string]License var Licenses map[string]License
@ -31,18 +36,6 @@ type License struct {
Header string // License header for source files 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() { func init() {
Licenses = make(map[string]License) 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 ""
}