diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go index 7cd3be1..6989bd7 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/cmd/helpers.go @@ -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") diff --git a/cobra/cmd/licenses.go b/cobra/cmd/licenses.go index 5b3cb72..2bd5e7f 100644 --- a/cobra/cmd/licenses.go +++ b/cobra/cmd/licenses.go @@ -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 "" +}