This commit is contained in:
Ian Walter 2016-04-27 16:02:05 +00:00
commit 496a12b27e
5 changed files with 82 additions and 47 deletions

View file

@ -226,13 +226,27 @@ The cobra generator will be easier to use if you provide a simple configuration
file which will help you eliminate providing a bunch of repeated information in
flags over and over.
an example ~/.cobra.yaml file:
An example ~/.cobra.yaml file:
```yaml
author: Steve Francia <spf@spf13.com>
license: MIT
```
You can specify no license by setting `license` to `none` or you can specify
a custom license:
```yaml
license:
header: This file is part of {{ .appName }}.
text: |
{{ .copyright }}
This is my license. There are many like it, but this one is mine.
My license is my best friend. It is my life. I must master it as I must
master my life.
```
## Manually implementing Cobra
To manually implement cobra you need to create a bare main.go file and a RootCmd file.

View file

@ -319,6 +319,15 @@ func whichLicense() string {
// 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"))
}

View file

@ -14,6 +14,7 @@
package cmd
import (
"bytes"
"fmt"
"os"
"strings"
@ -89,27 +90,34 @@ func initializePath(path string) {
func createLicenseFile() {
lic := getLicense()
template := lic.Text
// Don't bother writing a LICENSE file if there is no text.
if lic.Text != "" {
data := make(map[string]interface{})
var data map[string]interface{}
data = make(map[string]interface{})
// Try to remove the email address, if any
data["copyright"] = strings.Split(copyrightLine(), " <")[0]
// Try to remove the email address, if any
data["copyright"] = strings.Split(copyrightLine(), " <")[0]
data["appName"] = projectName()
err := writeTemplateToFile(ProjectPath(), "LICENSE", template, data)
_ = err
// if err != nil {
// er(err)
// }
// Generate license template from text and data.
r, _ := templateToReader(lic.Text, data)
buf := new(bytes.Buffer)
buf.ReadFrom(r)
err := writeTemplateToFile(ProjectPath(), "LICENSE", buf.String(), data)
_ = err
// if err != nil {
// er(err)
// }
}
}
func createMainFile() {
lic := getLicense()
template := `{{ comment .copyright }}
{{ comment .license }}
{{if .license}}{{ comment .license }}
{{end}}
package main
import "{{ .importpath }}"
@ -118,11 +126,17 @@ func main() {
cmd.Execute()
}
`
var data map[string]interface{}
data = make(map[string]interface{})
data := make(map[string]interface{})
data["copyright"] = copyrightLine()
data["license"] = lic.Header
data["appName"] = projectName()
// Generate license template from header and data.
r, _ := templateToReader(lic.Header, data)
buf := new(bytes.Buffer)
buf.ReadFrom(r)
data["license"] = buf.String()
data["importpath"] = guessImportPath() + "/" + guessCmdDir()
err := writeTemplateToFile(ProjectPath(), "main.go", template, data)
@ -136,8 +150,8 @@ func createRootCmdFile() {
lic := getLicense()
template := `{{ comment .copyright }}
{{ comment .license }}
{{if .license}}{{ comment .license }}
{{end}}
package cmd
import (
@ -206,12 +220,17 @@ func initConfig() {
}
{{ end }}`
var data map[string]interface{}
data = make(map[string]interface{})
data := make(map[string]interface{})
data["copyright"] = copyrightLine()
data["license"] = lic.Header
data["appName"] = projectName()
// Generate license template from header and data.
r, _ := templateToReader(lic.Header, data)
buf := new(bytes.Buffer)
buf.ReadFrom(r)
data["license"] = buf.String()
data["viper"] = viper.GetBool("useViper")
err := writeTemplateToFile(ProjectPath()+string(os.PathSeparator)+guessCmdDir(), "root.go", template, data)

View file

@ -46,6 +46,12 @@ func matchLicense(in string) string {
func init() {
Licenses = make(map[string]License)
// Allows a user to not use a license.
Licenses["none"] = License{"None", []string{"none", "false"}, "", ""}
// Allows a user to use config for a custom license.
Licenses["custom"] = License{"Custom", []string{}, "", ""}
Licenses["apache"] = License{
Name: "Apache 2.0",
PossibleMatches: []string{"apache", "apache20", "apache 2.0", "apache2.0", "apache-2.0"},
@ -727,22 +733,21 @@ POSSIBILITY OF SUCH DAMAGES.
Licenses["gpl3"] = License{
Name: "GNU General Public License 3.0",
PossibleMatches: []string{"gpl3", "gpl", "gnu gpl3", "gnu gpl"},
Header: `{{ .copyright }}
Header: `
This file is part of {{ .appName }}.
This file is part of {{ .appName }}.
{{ .appName }} is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
{{ .appName }} is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
{{ .appName }} is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
{{ .appName }} is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with {{ .appName }}. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with {{ .appName }}. If not, see <http://www.gnu.org/licenses/>.
`,
Text: ` GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

View file

@ -46,25 +46,13 @@ func init() {
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory, e.g. github.com/spf13/")
RootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution")
RootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `licensetext` in config)")
RootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `license` in config)")
RootCmd.PersistentFlags().Bool("viper", true, "Use Viper for configuration")
viper.BindPFlag("author", RootCmd.PersistentFlags().Lookup("author"))
viper.BindPFlag("projectbase", RootCmd.PersistentFlags().Lookup("projectbase"))
viper.BindPFlag("useViper", RootCmd.PersistentFlags().Lookup("viper"))
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
viper.SetDefault("license", "apache")
viper.SetDefault("licenseText", `
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
`)
}
// Read in config file and ENV variables if set.