cmd: Move some license functions from helpers.go to licenses.go
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent 28fe9aa · Browse files · 2 files·+59 added−57 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+59 added−57 removed
- other:+59 added−57 removed
Provenance
- Commit
78a8032- Tree
a50c405- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
28fe9aa
2 files changed+59 added−57 removed
modified:cobra/cmd/helpers.go-44 removed
19 unmodified lines20 "os"21 "path/filepath"22 "strings"23 "text/template"24- "time"25-26- "github.com/spf13/viper"24 )25 26 // var BaseDir = ""27 // var AppName = ""265 unmodified lines293 _, err = io.Copy(file, r)294 return295 }296 300-func getLicense() License {301- l := whichLicense()302- if l != "" {303- if x, ok := Licenses[l]; ok {304- return x305- }306- }307-308- return Licenses["apache"]309-}310-311-func whichLicense() string {312- // if explicitly flagged, use that313- if userLicense != "" {314- return matchLicense(userLicense)315- }316-317- // if already present in the project, use that318- // TODO: Inspect project for existing license319-320- // default to viper's setting321-322- if viper.IsSet("license.header") || viper.IsSet("license.text") {323- if custom, ok := Licenses["custom"]; ok {324- custom.Header = viper.GetString("license.header")325- custom.Text = viper.GetString("license.text")326- Licenses["custom"] = custom327- return "custom"328- }329- }330-331- return matchLicense(viper.GetString("license"))332-}333-334-func copyrightLine() string {335- author := viper.GetString("author")336- year := time.Now().Format("2006")337-338- return "Copyright © " + year + " " + author339-}340-297 func commentifyString(in string) string {298 var newlines []string299 lines := strings.Split(in, "\n")300 for _, x := range lines {12 unmodified linesmodified:cobra/cmd/licenses.go+59 added-13 removed
13 unmodified lines14 // Parts inspired by https://github.com/ryanuber/go-license15 16 package cmd17 18-import "strings"18+import (19+ "strings"20+ "time"21 22+ "github.com/spf13/viper"23+)24+25 //Licenses contains all possible licenses a user can chose from26 var Licenses map[string]License27 28 //License represents a software license agreement, containing the Name of6 unmodified lines35 Text string // License text data36 Header string // License header for source files37 }38 34-// given a license name (in), try to match the license indicated35-func matchLicense(in string) string {36- for key, lic := range Licenses {37- for _, match := range lic.PossibleMatches {38- if strings.EqualFold(in, match) {39- return key40- }41- }42- }43- return ""44-}45-39 func init() {40 Licenses = make(map[string]License)41 42 // Allows a user to not use a license.22 unmodified lines65 // Text: `66 // `,67 // }68 }69+70+func getLicense() License {71+ l := whichLicense()72+ if l != "" {73+ if x, ok := Licenses[l]; ok {74+ return x75+ }76+ }77+78+ return Licenses["apache"]79+}80+81+func whichLicense() string {82+ // if explicitly flagged, use that83+ if userLicense != "" {84+ return matchLicense(userLicense)85+ }86+87+ // if already present in the project, use that88+ // TODO: Inspect project for existing license89+90+ // default to viper's setting91+92+ if viper.IsSet("license.header") || viper.IsSet("license.text") {93+ if custom, ok := Licenses["custom"]; ok {94+ custom.Header = viper.GetString("license.header")95+ custom.Text = viper.GetString("license.text")96+ Licenses["custom"] = custom97+ return "custom"98+ }99+ }100+101+ return matchLicense(viper.GetString("license"))102+}103+104+func copyrightLine() string {105+ author := viper.GetString("author")106+ year := time.Now().Format("2006")107+108+ return "Copyright © " + year + " " + author109+}110+111+// given a license name (in), try to match the license indicated112+func matchLicense(in string) string {113+ for key, lic := range Licenses {114+ for _, match := range lic.PossibleMatches {115+ if strings.EqualFold(in, match) {116+ return key117+ }118+ }119+ }120+ return ""121+}