2015-10-28 16:51:48 +00:00
|
|
|
// Copyright © 2015 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2016-03-31 18:31:03 +00:00
|
|
|
"bytes"
|
2015-10-28 17:45:33 +00:00
|
|
|
"fmt"
|
2015-10-28 16:51:48 +00:00
|
|
|
"os"
|
2015-11-22 06:29:20 +00:00
|
|
|
"strings"
|
2015-10-28 16:51:48 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(initCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize Command
|
|
|
|
var initCmd = &cobra.Command{
|
|
|
|
Use: "init [name]",
|
2015-12-17 18:18:55 +00:00
|
|
|
Aliases: []string{"initialize", "initialise", "create"},
|
|
|
|
Short: "Initialize a Cobra Application",
|
2015-11-20 22:28:40 +00:00
|
|
|
Long: `Initialize (cobra init) will create a new application, with a license
|
|
|
|
and the appropriate structure for a Cobra-based CLI application.
|
2015-10-28 16:51:48 +00:00
|
|
|
|
2015-11-20 22:28:40 +00:00
|
|
|
* If a name is provided, it will be created in the current directory;
|
|
|
|
* If no name is provided, the current directory will be assumed;
|
|
|
|
* If a relative path is provided, it will be created inside $GOPATH
|
|
|
|
(e.g. github.com/spf13/hugo);
|
|
|
|
* If an absolute path is provided, it will be created;
|
|
|
|
* If the directory already exists but is empty, it will be used.
|
2015-10-28 16:51:48 +00:00
|
|
|
|
2015-12-17 21:43:23 +00:00
|
|
|
Init will not use an existing directory with contents.`,
|
2015-10-28 16:51:48 +00:00
|
|
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
inputPath = ""
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
inputPath = args[0]
|
|
|
|
|
|
|
|
default:
|
|
|
|
er("init doesn't support more than 1 parameter")
|
|
|
|
}
|
|
|
|
guessProjectPath()
|
2015-12-17 18:18:55 +00:00
|
|
|
initializePath(projectPath)
|
2015-10-28 16:51:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-12-17 18:18:55 +00:00
|
|
|
func initializePath(path string) {
|
2015-10-28 16:51:48 +00:00
|
|
|
b, err := exists(path)
|
|
|
|
if err != nil {
|
|
|
|
er(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !b { // If path doesn't yet exist, create it
|
|
|
|
err := os.MkdirAll(path, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
er(err)
|
|
|
|
}
|
|
|
|
} else { // If path exists and is not empty don't use it
|
|
|
|
empty, err := exists(path)
|
|
|
|
if err != nil {
|
|
|
|
er(err)
|
|
|
|
}
|
|
|
|
if !empty {
|
|
|
|
er("Cobra will not create a new project in a non empty directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We have a directory and it's empty.. Time to initialize it.
|
|
|
|
|
|
|
|
createLicenseFile()
|
|
|
|
createMainFile()
|
|
|
|
createRootCmdFile()
|
|
|
|
}
|
|
|
|
|
|
|
|
func createLicenseFile() {
|
2016-03-31 18:52:16 +00:00
|
|
|
lic := getLicense()
|
2015-11-22 06:29:20 +00:00
|
|
|
|
2016-03-31 18:52:16 +00:00
|
|
|
// Don't bother writing a LICENSE file if there is no text.
|
|
|
|
if lic.Text != "" {
|
|
|
|
data := make(map[string]interface{})
|
2015-11-22 06:29:20 +00:00
|
|
|
|
2016-03-31 18:52:16 +00:00
|
|
|
// Try to remove the email address, if any
|
|
|
|
data["copyright"] = strings.Split(copyrightLine(), " <")[0]
|
2016-03-31 18:31:03 +00:00
|
|
|
|
2016-03-31 18:52:16 +00:00
|
|
|
data["appName"] = projectName()
|
|
|
|
|
|
|
|
// Generate license template from text and data.
|
|
|
|
r, _ := templateToReader(lic.Text, data)
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(r)
|
|
|
|
|
2016-05-03 02:37:29 +00:00
|
|
|
err := writeTemplateToFile(ProjectPath(), "LICENSE", buf.String(), data)
|
2016-03-31 18:31:03 +00:00
|
|
|
_ = err
|
|
|
|
// if err != nil {
|
|
|
|
// er(err)
|
|
|
|
// }
|
|
|
|
}
|
2015-10-28 16:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createMainFile() {
|
|
|
|
lic := getLicense()
|
|
|
|
|
|
|
|
template := `{{ comment .copyright }}
|
2016-03-31 18:31:03 +00:00
|
|
|
{{if .license}}{{ comment .license }}
|
|
|
|
{{end}}
|
2015-10-28 16:51:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "{{ .importpath }}"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cmd.Execute()
|
|
|
|
}
|
|
|
|
`
|
2016-03-31 18:31:03 +00:00
|
|
|
data := make(map[string]interface{})
|
2015-10-28 16:51:48 +00:00
|
|
|
|
|
|
|
data["copyright"] = copyrightLine()
|
2016-03-31 18:31:03 +00:00
|
|
|
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()
|
|
|
|
|
2015-10-28 16:51:48 +00:00
|
|
|
data["importpath"] = guessImportPath() + "/" + guessCmdDir()
|
|
|
|
|
2016-05-03 02:37:29 +00:00
|
|
|
err := writeTemplateToFile(ProjectPath(), "main.go", template, data)
|
2015-10-28 16:51:48 +00:00
|
|
|
_ = err
|
|
|
|
// if err != nil {
|
|
|
|
// er(err)
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRootCmdFile() {
|
|
|
|
lic := getLicense()
|
|
|
|
|
|
|
|
template := `{{ comment .copyright }}
|
2016-03-31 18:31:03 +00:00
|
|
|
{{if .license}}{{ comment .license }}
|
|
|
|
{{end}}
|
2015-10-28 16:51:48 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2015-11-20 22:28:40 +00:00
|
|
|
{{ if .viper }} "github.com/spf13/viper"
|
|
|
|
{{ end }})
|
2015-10-28 16:51:48 +00:00
|
|
|
{{if .viper}}
|
|
|
|
var cfgFile string
|
|
|
|
{{ end }}
|
2016-04-02 01:29:12 +00:00
|
|
|
// RootCmd represents the base command when called without any subcommands
|
2015-10-28 16:51:48 +00:00
|
|
|
var RootCmd = &cobra.Command{
|
|
|
|
Use: "{{ .appName }}",
|
|
|
|
Short: "A brief description of your application",
|
2015-11-20 22:28:40 +00:00
|
|
|
Long: ` + "`" + `A longer description that spans multiple lines and likely contains
|
|
|
|
examples and usage of using your application. For example:
|
|
|
|
|
|
|
|
Cobra is a CLI library for Go that empowers applications.
|
|
|
|
This application is a tool to generate the needed files
|
|
|
|
to quickly create a Cobra application.` + "`" + `,
|
|
|
|
// Uncomment the following line if your bare application
|
|
|
|
// has an action associated with it:
|
2015-10-28 16:51:48 +00:00
|
|
|
// Run: func(cmd *cobra.Command, args []string) { },
|
|
|
|
}
|
|
|
|
|
2015-11-20 22:28:40 +00:00
|
|
|
// Execute adds all child commands to the root command sets flags appropriately.
|
|
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
2015-10-28 16:51:48 +00:00
|
|
|
func Execute() {
|
|
|
|
if err := RootCmd.Execute(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2015-11-20 22:28:40 +00:00
|
|
|
{{ if .viper }} cobra.OnInitialize(initConfig)
|
2015-10-28 16:51:48 +00:00
|
|
|
|
2015-11-20 22:28:40 +00:00
|
|
|
{{ end }} // Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags, which, if defined here,
|
|
|
|
// will be global for your application.
|
|
|
|
{{ if .viper }}
|
|
|
|
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)")
|
|
|
|
{{ else }}
|
|
|
|
// RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)")
|
|
|
|
{{ end }} // Cobra also supports local flags, which will only run
|
|
|
|
// when this action is called directly.
|
|
|
|
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
2015-10-28 16:51:48 +00:00
|
|
|
}
|
|
|
|
{{ if .viper }}
|
2015-11-20 22:28:40 +00:00
|
|
|
// initConfig reads in config file and ENV variables if set.
|
2015-10-28 16:51:48 +00:00
|
|
|
func initConfig() {
|
|
|
|
if cfgFile != "" { // enable ability to specify config file via flag
|
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.SetConfigName(".{{ .appName }}") // name of config file (without extension)
|
|
|
|
viper.AddConfigPath("$HOME") // adding home directory as first search path
|
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
|
|
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 22:28:40 +00:00
|
|
|
{{ end }}`
|
2015-10-28 16:51:48 +00:00
|
|
|
|
2016-03-31 18:31:03 +00:00
|
|
|
data := make(map[string]interface{})
|
2015-10-28 16:51:48 +00:00
|
|
|
|
|
|
|
data["copyright"] = copyrightLine()
|
|
|
|
data["appName"] = projectName()
|
2016-03-31 18:31:03 +00:00
|
|
|
|
|
|
|
// Generate license template from header and data.
|
|
|
|
r, _ := templateToReader(lic.Header, data)
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(r)
|
|
|
|
data["license"] = buf.String()
|
|
|
|
|
2015-10-28 16:51:48 +00:00
|
|
|
data["viper"] = viper.GetBool("useViper")
|
|
|
|
|
2016-05-03 02:37:29 +00:00
|
|
|
path := ProjectPath() + string(os.PathSeparator) + guessCmdDir()
|
|
|
|
if err := writeTemplateToFile(path, "root.go", template, data); err != nil {
|
2015-10-28 16:51:48 +00:00
|
|
|
er(err)
|
|
|
|
}
|
2015-10-28 17:45:33 +00:00
|
|
|
|
2015-11-20 22:49:08 +00:00
|
|
|
fmt.Println("Your Cobra application is ready at")
|
2015-10-28 17:45:33 +00:00
|
|
|
fmt.Println(ProjectPath())
|
|
|
|
fmt.Println("Give it a try by going there and running `go run main.go`")
|
|
|
|
fmt.Println("Add commands to it by running `cobra add [cmdname]`")
|
2015-10-28 16:51:48 +00:00
|
|
|
}
|