From c7ac101cf82f9a5660532cdd00fb5e908b359c29 Mon Sep 17 00:00:00 2001 From: jharshman Date: Wed, 30 Jan 2019 01:24:26 -0800 Subject: [PATCH] vgo - fixing up the add op to work with vgo --- cobra/cmd/add.go | 129 +++++++++++++++---------------------------- cobra/cmd/project.go | 12 +++- cobra/tpl/main.go | 2 +- 3 files changed, 54 insertions(+), 89 deletions(-) diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index fb22096a..ca19cb71 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -16,24 +16,21 @@ package cmd import ( "fmt" "os" - "path/filepath" + "path" "unicode" "github.com/spf13/cobra" ) -func init() { - addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)") - addCmd.Flags().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command") -} +var ( + packageName string + parentName string -var packageName, parentName string - -var addCmd = &cobra.Command{ - Use: "add [command name]", - Aliases: []string{"command"}, - Short: "Add a command to a Cobra Application", - Long: `Add (cobra add) will create a new command, with a license and + addCmd = &cobra.Command{ + Use: "add [command name]", + Aliases: []string{"command"}, + Short: "Add a command to a Cobra Application", + Long: `Add (cobra add) will create a new command, with a license and the appropriate structure for a Cobra-based CLI application, and register it to its parent (default rootCmd). @@ -42,28 +39,47 @@ with an initial uppercase letter. Example: cobra add server -> resulting in a new cmd/server.go`, - Run: func(cmd *cobra.Command, args []string) { - if len(args) < 1 { - er("add needs a name for the command") - } + Run: func(cmd *cobra.Command, args []string) { + if len(args) < 1 { + er("add needs a name for the command") + } + + commandName := validateCmdName(args[0]) + + if packageName == "" { + // derive packageName + } - var project *Project - if packageName != "" { - project = NewProject(packageName) - } else { wd, err := os.Getwd() if err != nil { er(err) } - project = NewProjectFromPath(wd) - } - cmdName := validateCmdName(args[0]) - cmdPath := filepath.Join(project.CmdPath(), cmdName+".go") - createCmdFile(project.License(), cmdPath, cmdName) + command := &Command{ + CmdName: commandName, + CmdParent: parentName, + Project: &Project{ + AbsolutePath: fmt.Sprintf("%s/cmd", wd), + AppName: path.Base(packageName), + PkgName: packageName, + Legal: getLicense(), + Copyright: copyrightLine(), + }, + } - fmt.Fprintln(cmd.OutOrStdout(), cmdName, "created at", cmdPath) - }, + err = command.Create() + if err != nil { + er(err) + } + + fmt.Printf("%s created at %s", command.CmdName, command.Project.AbsolutePath) + }, + } +) + +func init() { + addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)") + addCmd.Flags().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command") } // validateCmdName returns source without any dashes and underscore. @@ -118,62 +134,3 @@ func validateCmdName(source string) string { } return output } - -func createCmdFile(license License, path, cmdName string) { - template := `{{comment .copyright}} -{{if .license}}{{comment .license}}{{end}} - -package {{.cmdPackage}} - -import ( - "fmt" - - "github.com/spf13/cobra" -) - -// {{.cmdName}}Cmd represents the {{.cmdName}} command -var {{.cmdName}}Cmd = &cobra.Command{ - Use: "{{.cmdName}}", - Short: "A brief description of your command", - Long: ` + "`" + `A longer description that spans multiple lines and likely contains examples -and usage of using your command. 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.` + "`" + `, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("{{.cmdName}} called") - }, -} - -func init() { - {{.parentName}}.AddCommand({{.cmdName}}Cmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // {{.cmdName}}Cmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // {{.cmdName}}Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} -` - - data := make(map[string]interface{}) - data["copyright"] = copyrightLine() - data["license"] = license.Header - data["cmdPackage"] = filepath.Base(filepath.Dir(path)) // last dir of path - data["parentName"] = parentName - data["cmdName"] = cmdName - - cmdScript, err := executeTemplate(template, data) - if err != nil { - er(err) - } - err = writeStringToFile(path, cmdScript) - if err != nil { - er(err) - } -} diff --git a/cobra/cmd/project.go b/cobra/cmd/project.go index 789a2490..fe9ea319 100644 --- a/cobra/cmd/project.go +++ b/cobra/cmd/project.go @@ -19,8 +19,6 @@ type Project struct { Legal License Viper bool AppName string - CmdName string - CmdParent string // v1 absPath string @@ -30,6 +28,12 @@ type Project struct { name string } +type Command struct { + CmdName string + CmdParent string + *Project +} + func (p *Project) Create() error { // check if AbsolutePath exists @@ -86,6 +90,10 @@ func (p *Project) createLicenseFile() error { return licenseTemplate.Execute(licenseFile, data) } +func (c *Command) Create() error { + return nil +} + // NewProject returns Project with specified project name. func NewProject(projectName string) *Project { if projectName == "" { diff --git a/cobra/tpl/main.go b/cobra/tpl/main.go index 1b8ff10a..ba4d7cc5 100644 --- a/cobra/tpl/main.go +++ b/cobra/tpl/main.go @@ -150,4 +150,4 @@ func init() { // {{ .CmdName }}Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } `) -} \ No newline at end of file +}