vgo - fixing up the add op to work with vgo

This commit is contained in:
jharshman 2019-01-30 01:24:26 -08:00 committed by Steve Francia
parent 3741457400
commit c7ac101cf8
3 changed files with 54 additions and 89 deletions

View file

@ -16,24 +16,21 @@ package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path"
"unicode" "unicode"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func init() { var (
addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)") packageName string
addCmd.Flags().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command") parentName string
}
var packageName, parentName string addCmd = &cobra.Command{
Use: "add [command name]",
var addCmd = &cobra.Command{ Aliases: []string{"command"},
Use: "add [command name]", Short: "Add a command to a Cobra Application",
Aliases: []string{"command"}, Long: `Add (cobra add) will create a new command, with a license and
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, the appropriate structure for a Cobra-based CLI application,
and register it to its parent (default rootCmd). 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`, Example: cobra add server -> resulting in a new cmd/server.go`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 { if len(args) < 1 {
er("add needs a name for the command") 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() wd, err := os.Getwd()
if err != nil { if err != nil {
er(err) er(err)
} }
project = NewProjectFromPath(wd)
}
cmdName := validateCmdName(args[0]) command := &Command{
cmdPath := filepath.Join(project.CmdPath(), cmdName+".go") CmdName: commandName,
createCmdFile(project.License(), cmdPath, cmdName) 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. // validateCmdName returns source without any dashes and underscore.
@ -118,62 +134,3 @@ func validateCmdName(source string) string {
} }
return output 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)
}
}

View file

@ -19,8 +19,6 @@ type Project struct {
Legal License Legal License
Viper bool Viper bool
AppName string AppName string
CmdName string
CmdParent string
// v1 // v1
absPath string absPath string
@ -30,6 +28,12 @@ type Project struct {
name string name string
} }
type Command struct {
CmdName string
CmdParent string
*Project
}
func (p *Project) Create() error { func (p *Project) Create() error {
// check if AbsolutePath exists // check if AbsolutePath exists
@ -86,6 +90,10 @@ func (p *Project) createLicenseFile() error {
return licenseTemplate.Execute(licenseFile, data) return licenseTemplate.Execute(licenseFile, data)
} }
func (c *Command) Create() error {
return nil
}
// NewProject returns Project with specified project name. // NewProject returns Project with specified project name.
func NewProject(projectName string) *Project { func NewProject(projectName string) *Project {
if projectName == "" { if projectName == "" {

View file

@ -150,4 +150,4 @@ func init() {
// {{ .CmdName }}Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") // {{ .CmdName }}Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }
`) `)
} }