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,20 +16,17 @@ 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{
addCmd = &cobra.Command{
Use: "add [command name]",
Aliases: []string{"command"},
Short: "Add a command to a Cobra Application",
@ -47,23 +44,42 @@ Example: cobra add server -> resulting in a new cmd/server.go`,
er("add needs a name for the command")
}
var project *Project
if packageName != "" {
project = NewProject(packageName)
} else {
commandName := validateCmdName(args[0])
if packageName == "" {
// derive packageName
}
wd, err := os.Getwd()
if err != nil {
er(err)
}
project = NewProjectFromPath(wd)
command := &Command{
CmdName: commandName,
CmdParent: parentName,
Project: &Project{
AbsolutePath: fmt.Sprintf("%s/cmd", wd),
AppName: path.Base(packageName),
PkgName: packageName,
Legal: getLicense(),
Copyright: copyrightLine(),
},
}
cmdName := validateCmdName(args[0])
cmdPath := filepath.Join(project.CmdPath(), cmdName+".go")
createCmdFile(project.License(), cmdPath, cmdName)
err = command.Create()
if err != nil {
er(err)
}
fmt.Fprintln(cmd.OutOrStdout(), cmdName, "created at", cmdPath)
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)
}
}

View file

@ -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 == "" {