Adding custom license functionality
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Refactoring code that unnecessarily declares a map before making it. * Cleaning up gpl3 formatting to match other licenses. * Adding functionality that allows specifying custom license header and text in cobra config. * Using license header and text as templates so that they can use template variables (for custom and gpl3 licenses). * Adding ability to specify no license. * Adding custom license example to README.
authored by Ian Walter · committed by Steve Francia · parent 3dc021e · Browse files · 5 files·+80 added−48 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+80 added−48 removed
- other:+80 added−48 removed
Provenance
- Commit
d811f40- Tree
db6001b- Author
Ian Walter <public@iankwalter.com>- Committer
Steve Francia <steve.francia@gmail.com>- Parent
3dc021e
5 files changed+80 added−48 removed
modified:cobra/cmd/helpers.go+9 added
317 unmodified lines318 // 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 {22 unmodified linesmodified:cobra/cmd/init.go+37 added-21 removed
12 unmodified lines13 14 package cmd15 16 import (17+ "bytes"18 "fmt"19 "os"20 "strings"21 65 unmodified lines87 createRootCmdFile()88 }89 90 func createLicenseFile() {90- lic := getLicense()91+ data := make(map[string]interface{})92 92- template := lic.Text93-94- var data map[string]interface{}95- data = make(map[string]interface{})96-93 // Try to remove the email address, if any94 data["copyright"] = strings.Split(copyrightLine(), " <")[0]95 96+ data["appName"] = projectName()97+98+ // Get license and generate the template from text and data.99+ lic := getLicense()100+ r, _ := templateToReader(lic.Text, data)101+ buf := new(bytes.Buffer)102+ buf.ReadFrom(r)103+ if template := buf.String(); template != "" {100- err := writeTemplateToFile(ProjectPath(), "LICENSE", template, data)101- _ = err102- // if err != nil {103- // er(err)104- // }104+ err := writeTemplateToFile(ProjectPath(), "LICENSE", template, data)105+ _ = err106+ // if err != nil {107+ // er(err)108+ // }109+ }110 }111 112 func createMainFile() {113 lic := getLicense()114 115 template := `{{ comment .copyright }}111-{{ comment .license }}112-116+{{if .license}}{{ comment .license }}117+{{end}}118 package main119 120 import "{{ .importpath }}"121 122 func main() {123 cmd.Execute()124 }125 `121- var data map[string]interface{}122- data = make(map[string]interface{})126+ data := make(map[string]interface{})127 128 data["copyright"] = copyrightLine()125- data["license"] = lic.Header129+ data["appName"] = projectName()130+131+ // Generate license template from header and data.132+ r, _ := templateToReader(lic.Header, data)133+ buf := new(bytes.Buffer)134+ buf.ReadFrom(r)135+ data["license"] = buf.String()136+137 data["importpath"] = guessImportPath() + "/" + guessCmdDir()138 139 err := writeTemplateToFile(ProjectPath(), "main.go", template, data)140 _ = err5 unmodified lines146 func createRootCmdFile() {147 lic := getLicense()148 149 template := `{{ comment .copyright }}139-{{ comment .license }}140-150+{{if .license}}{{ comment .license }}151+{{end}}152 package cmd153 154 import (155 "fmt"60 unmodified lines216 }217 }218 {{ end }}`219 209- var data map[string]interface{}210- data = make(map[string]interface{})220+ data := make(map[string]interface{})221 222 data["copyright"] = copyrightLine()213- data["license"] = lic.Header223 data["appName"] = projectName()224+225+ // Generate license template from header and data.226+ r, _ := templateToReader(lic.Header, data)227+ buf := new(bytes.Buffer)228+ buf.ReadFrom(r)229+ data["license"] = buf.String()230+231 data["viper"] = viper.GetBool("useViper")232 233 err := writeTemplateToFile(ProjectPath()+string(os.PathSeparator)+guessCmdDir(), "root.go", template, data)234 if err != nil {8 unmodified linesmodified:cobra/cmd/licenses.go+18 added-13 removed
44 unmodified lines45 46 func init() {47 Licenses = make(map[string]License)48 49+ // Allows a user to not use a license.50+ Licenses["none"] = License{"None", []string{"none", "false"}, "", ""}51+52+ // Allows a user to use config for a custom license.53+ Licenses["custom"] = License{"Custom", []string{}, "", ""}54+55 Licenses["apache"] = License{56 Name: "Apache 2.0",57 PossibleMatches: []string{"apache", "apache20", "apache 2.0", "apache2.0", "apache-2.0"},58 Header: `673 unmodified lines732 }733 Licenses["gpl3"] = License{734 Name: "GNU General Public License 3.0",735 PossibleMatches: []string{"gpl3", "gpl", "gnu gpl3", "gnu gpl"},730- Header: `{{ .copyright }}736+ Header: `737+This file is part of {{ .appName }}.738 732- This file is part of {{ .appName }}.739+{{ .appName }} is free software: you can redistribute it and/or modify740+it under the terms of the GNU Lesser General Public License as published by741+the Free Software Foundation, either version 3 of the License, or742+(at your option) any later version.743 734- {{ .appName }} is free software: you can redistribute it and/or modify735- it under the terms of the GNU Lesser General Public License as published by736- the Free Software Foundation, either version 3 of the License, or737- (at your option) any later version.744+{{ .appName }} is distributed in the hope that it will be useful,745+but WITHOUT ANY WARRANTY; without even the implied warranty of746+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the747+GNU Lesser General Public License for more details.748 739- {{ .appName }} is distributed in the hope that it will be useful,740- but WITHOUT ANY WARRANTY; without even the implied warranty of741- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the742- GNU Lesser General Public License for more details.743-744- You should have received a copy of the GNU Lesser General Public License745- along with {{ .appName }}. If not, see <http://www.gnu.org/licenses/>.749+You should have received a copy of the GNU Lesser General Public License750+along with {{ .appName }}. If not, see <http://www.gnu.org/licenses/>.751 `,752 Text: ` GNU GENERAL PUBLIC LICENSE753 Version 3, 29 June 2007754 630 unmodified linesmodified:cobra/cmd/root.go+1 added-13 removed
44 unmodified lines45 cobra.OnInitialize(initConfig)46 RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")47 RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory, e.g. github.com/spf13/")48 RootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution")49- RootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `licensetext` in config)")49+ RootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `license` in config)")50 RootCmd.PersistentFlags().Bool("viper", true, "Use Viper for configuration")51 viper.BindPFlag("author", RootCmd.PersistentFlags().Lookup("author"))52 viper.BindPFlag("projectbase", RootCmd.PersistentFlags().Lookup("projectbase"))53 viper.BindPFlag("useViper", RootCmd.PersistentFlags().Lookup("viper"))54 viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")55 viper.SetDefault("license", "apache")56- viper.SetDefault("licenseText", `57-// Licensed under the Apache License, Version 2.0 (the "License");58-// you may not use this file except in compliance with the License.59-// You may obtain a copy of the License at60-// http://www.apache.org/licenses/LICENSE-2.061-//62-// Unless required by applicable law or agreed to in writing, software63-// distributed under the License is distributed on an "AS IS" BASIS,64-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.65-// See the License for the specific language governing permissions and66-// limitations under the License.67-`)56 }57 58 // Read in config file and ENV variables if set.59 func initConfig() {13 unmodified linesmodified:README.md+15 added-1 removed
224 unmodified lines225 The cobra generator will be easier to use if you provide a simple configuration226 file which will help you eliminate providing a bunch of repeated information in227 flags over and over.228 229-an example ~/.cobra.yaml file:229+An example ~/.cobra.yaml file:230 231 ```yaml232 author: Steve Francia <spf@spf13.com>233 license: MIT234 ```235 236+You can specify no license by setting `license` to `none` or you can specify237+a custom license:238+239+```yaml240+license:241+ header: This file is part of {{ .appName }}.242+ text: |243+ {{ .Copyright }}244+245+ This is my license. There are many like it, but this one is mine.246+ My license is my best friend. It is my life. I must master it as I must247+ master my life. 248+```249+250 ## Manually implementing Cobra251 252 To manually implement cobra you need to create a bare main.go file and a RootCmd file.253 You will optionally provide additional commands as you see fit.638 unmodified lines