diff options
Diffstat (limited to 'vendor/github.com/spf13/cobra/cobra/cmd/init.go')
-rw-r--r-- | vendor/github.com/spf13/cobra/cobra/cmd/init.go | 235 |
1 files changed, 112 insertions, 123 deletions
diff --git a/vendor/github.com/spf13/cobra/cobra/cmd/init.go b/vendor/github.com/spf13/cobra/cobra/cmd/init.go index b77c98645..0e2b22aa0 100644 --- a/vendor/github.com/spf13/cobra/cobra/cmd/init.go +++ b/vendor/github.com/spf13/cobra/cobra/cmd/init.go @@ -14,20 +14,15 @@ package cmd import ( - "bytes" "fmt" "os" - "strings" + "path" + "path/filepath" "github.com/spf13/cobra" "github.com/spf13/viper" ) -func init() { - RootCmd.AddCommand(initCmd) -} - -// initialize Command var initCmd = &cobra.Command{ Use: "init [name]", Aliases: []string{"initialize", "initialise", "create"}, @@ -45,79 +40,75 @@ and the appropriate structure for a Cobra-based CLI application. Init will not use an existing directory with contents.`, Run: func(cmd *cobra.Command, args []string) { - switch len(args) { - case 0: - inputPath = "" - - case 1: - inputPath = args[0] + wd, err := os.Getwd() + if err != nil { + er(err) + } - default: - er("init doesn't support more than 1 parameter") + var project *Project + if len(args) == 0 { + project = NewProjectFromPath(wd) + } else if len(args) == 1 { + arg := args[0] + if arg[0] == '.' { + arg = filepath.Join(wd, arg) + } + if filepath.IsAbs(arg) { + project = NewProjectFromPath(arg) + } else { + project = NewProject(arg) + } + } else { + er("please enter the name") } - guessProjectPath() - initializePath(projectPath) + + initializeProject(project) + + fmt.Fprintln(cmd.OutOrStdout(), `Your Cobra application is ready at +`+project.AbsPath()+`. + +Give it a try by going there and running `+"`go run main.go`."+` +Add commands to it by running `+"`cobra add [cmdname]`.") }, } -func initializePath(path string) { - 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) +func initializeProject(project *Project) { + if !exists(project.AbsPath()) { // If path doesn't yet exist, create it + err := os.MkdirAll(project.AbsPath(), 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") - } + } else if !isEmpty(project.AbsPath()) { // If path exists and is not empty don't use it + er("Cobra will not create a new project in a non empty directory: " + project.AbsPath()) } - // We have a directory and it's empty.. Time to initialize it. - createLicenseFile() - createMainFile() - createRootCmdFile() + // We have a directory and it's empty. Time to initialize it. + createLicenseFile(project.License(), project.AbsPath()) + createMainFile(project) + createRootCmdFile(project) } -func createLicenseFile() { - lic := getLicense() - - // Don't bother writing a LICENSE file if there is no text. - if lic.Text != "" { - data := make(map[string]interface{}) - - // Try to remove the email address, if any - data["copyright"] = strings.Split(copyrightLine(), " <")[0] - - data["appName"] = projectName() +func createLicenseFile(license License, path string) { + data := make(map[string]interface{}) + data["copyright"] = copyrightLine() - // Generate license template from text and data. - r, _ := templateToReader(lic.Text, data) - buf := new(bytes.Buffer) - buf.ReadFrom(r) + // Generate license template from text and data. + text, err := executeTemplate(license.Text, data) + if err != nil { + er(err) + } - err := writeTemplateToFile(ProjectPath(), "LICENSE", buf.String(), data) - _ = err - // if err != nil { - // er(err) - // } + // Write license text to LICENSE file. + err = writeStringToFile(filepath.Join(path, "LICENSE"), text) + if err != nil { + er(err) } } -func createMainFile() { - lic := getLicense() +func createMainFile(project *Project) { + mainTemplate := `{{ comment .copyright }} +{{if .license}}{{ comment .license }}{{end}} - template := `{{ comment .copyright }} -{{if .license}}{{ comment .license }} -{{end}} package main import "{{ .importpath }}" @@ -127,46 +118,41 @@ func main() { } ` data := make(map[string]interface{}) - data["copyright"] = copyrightLine() - 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["license"] = project.License().Header + data["importpath"] = path.Join(project.Name(), filepath.Base(project.CmdPath())) - data["importpath"] = guessImportPath() + "/" + guessCmdDir() + mainScript, err := executeTemplate(mainTemplate, data) + if err != nil { + er(err) + } - err := writeTemplateToFile(ProjectPath(), "main.go", template, data) - _ = err - // if err != nil { - // er(err) - // } + err = writeStringToFile(filepath.Join(project.AbsPath(), "main.go"), mainScript) + if err != nil { + er(err) + } } -func createRootCmdFile() { - lic := getLicense() +func createRootCmdFile(project *Project) { + template := `{{comment .copyright}} +{{if .license}}{{comment .license}}{{end}} - template := `{{ comment .copyright }} -{{if .license}}{{ comment .license }} -{{end}} package cmd import ( "fmt" "os" + homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" -{{ if .viper }} "github.com/spf13/viper" -{{ end }}) -{{if .viper}} -var cfgFile string -{{ end }} +{{if .viper}} "github.com/spf13/viper"{{end}} +) + +{{if .viper}}var cfgFile string{{end}} + // RootCmd represents the base command when called without any subcommands var RootCmd = &cobra.Command{ - Use: "{{ .appName }}", + Use: "{{.appName}}", Short: "A brief description of your application", Long: ` + "`" + `A longer description that spans multiple lines and likely contains examples and usage of using your application. For example: @@ -174,9 +160,9 @@ 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: -// Run: func(cmd *cobra.Command, args []string) { }, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, } // Execute adds all child commands to the root command sets flags appropriately. @@ -184,62 +170,65 @@ to quickly create a Cobra application.` + "`" + `, func Execute() { if err := RootCmd.Execute(); err != nil { fmt.Println(err) - os.Exit(-1) + os.Exit(1) } } func init() { -{{ if .viper }} cobra.OnInitialize(initConfig) - -{{ 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 +{{if .viper}} cobra.OnInitialize(initConfig){{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") -} -{{ if .viper }} +}{{ if .viper }} + // initConfig reads in config file and ENV variables if set. func initConfig() { - if cfgFile != "" { // enable ability to specify config file via flag + if cfgFile != "" { + // Use config file from the flag. viper.SetConfigFile(cfgFile) + } else { + // Find home directory. + home, err := homedir.Dir() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // Search config in home directory with name ".{{ .appName }}" (without extension). + viper.AddConfigPath(home) + viper.SetConfigName(".{{ .appName }}") } - viper.SetConfigName(".{{ .appName }}") // name of config file (without extension) - viper.AddConfigPath(os.Getenv("HOME")) // adding home directory as first search path - viper.AutomaticEnv() // read in environment variables that match + 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()) } -} -{{ end }}` +}{{ end }} +` data := make(map[string]interface{}) - data["copyright"] = copyrightLine() - 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") + data["license"] = project.License().Header + data["appName"] = path.Base(project.Name()) + + rootCmdScript, err := executeTemplate(template, data) + if err != nil { + er(err) + } - err := writeTemplateToFile(ProjectPath()+string(os.PathSeparator)+guessCmdDir(), "root.go", template, data) + err = writeStringToFile(filepath.Join(project.CmdPath(), "root.go"), rootCmdScript) if err != nil { er(err) } - fmt.Println("Your Cobra application is ready at") - 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]`") } |