Skip to content
Myr

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

5 files+80 added−48 removed

modified:cobra/cmd/helpers.go+9 added
317 unmodified lines
318 // TODO: Inspect project for existing license319320 // default to viper's setting321322 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 }330331 return matchLicense(viper.GetString("license"))332}333334func copyrightLine() string {
22 unmodified lines
modified:cobra/cmd/init.go+37 added-21 removed
12 unmodified lines
1314package cmd1516import (17 "bytes"18 "fmt"19 "os"20 "strings"21
65 unmodified lines
87 createRootCmdFile()88}8990func createLicenseFile() {90 lic := getLicense()91 data := make(map[string]interface{})9292 template := lic.Text9394 var data map[string]interface{}95 data = make(map[string]interface{})9693 // Try to remove the email address, if any94 data["copyright"] = strings.Split(copyrightLine(), " <")[0]9596 data["appName"] = projectName()9798 // 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}111112func createMainFile() {113 lic := getLicense()114115 template := `{{ comment .copyright }}111{{ comment .license }}112116{{if .license}}{{ comment .license }}117{{end}}118package main119120import "{{ .importpath }}"121122func main() {123 cmd.Execute()124}125`121 var data map[string]interface{}122 data = make(map[string]interface{})126 data := make(map[string]interface{})127128 data["copyright"] = copyrightLine()125 data["license"] = lic.Header129 data["appName"] = projectName()130131 // 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()136137 data["importpath"] = guessImportPath() + "/" + guessCmdDir()138139 err := writeTemplateToFile(ProjectPath(), "main.go", template, data)140 _ = err
5 unmodified lines
146func createRootCmdFile() {147 lic := getLicense()148149 template := `{{ comment .copyright }}139{{ comment .license }}140150{{if .license}}{{ comment .license }}151{{end}}152package cmd153154import (155 "fmt"
60 unmodified lines
216 }217}218{{ end }}`219209 var data map[string]interface{}210 data = make(map[string]interface{})220 data := make(map[string]interface{})221222 data["copyright"] = copyrightLine()213 data["license"] = lic.Header223 data["appName"] = projectName()224225 // 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()230231 data["viper"] = viper.GetBool("useViper")232233 err := writeTemplateToFile(ProjectPath()+string(os.PathSeparator)+guessCmdDir(), "root.go", template, data)234 if err != nil {
8 unmodified lines
modified:cobra/cmd/licenses.go+18 added-13 removed
44 unmodified lines
4546func init() {47 Licenses = make(map[string]License)4849 // Allows a user to not use a license.50 Licenses["none"] = License{"None", []string{"none", "false"}, "", ""}5152 // Allows a user to use config for a custom license.53 Licenses["custom"] = License{"Custom", []string{}, "", ""}5455 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 lines
732 }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: `737This file is part of {{ .appName }}.738732 This file is part of {{ .appName }}.739{{ .appName }} is free software: you can redistribute it and/or modify740it under the terms of the GNU Lesser General Public License as published by741the Free Software Foundation, either version 3 of the License, or742(at your option) any later version.743734 {{ .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,745but WITHOUT ANY WARRANTY; without even the implied warranty of746MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the747GNU Lesser General Public License for more details.748739 {{ .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.743744 You should have received a copy of the GNU Lesser General Public License745 along with {{ .appName }}. If not, see <http://www.gnu.org/licenses/>.749You should have received a copy of the GNU Lesser General Public License750along with {{ .appName }}. If not, see <http://www.gnu.org/licenses/>.751 `,752 Text: ` GNU GENERAL PUBLIC LICENSE753 Version 3, 29 June 2007754
630 unmodified lines
modified:cobra/cmd/root.go+1 added-13 removed
44 unmodified lines
45 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}5758// Read in config file and ENV variables if set.59func initConfig() {
13 unmodified lines
modified:README.md+15 added-1 removed
224 unmodified lines
225The cobra generator will be easier to use if you provide a simple configuration226file which will help you eliminate providing a bunch of repeated information in227flags over and over.228229an example ~/.cobra.yaml file:229An example ~/.cobra.yaml file:230231```yaml232author: Steve Francia <spf@spf13.com>233license: MIT234```235236You can specify no license by setting `license` to `none` or you can specify237a custom license:238239```yaml240license:241 header: This file is part of {{ .appName }}.242 text: |243 {{ .Copyright }}244245 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```249250## Manually implementing Cobra251252To manually implement cobra you need to create a bare main.go file and a RootCmd file.253You will optionally provide additional commands as you see fit.
638 unmodified lines