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 ( 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{
var addCmd = &cobra.Command{
Use: "add [command name]", Use: "add [command name]",
Aliases: []string{"command"}, Aliases: []string{"command"},
Short: "Add a command to a Cobra Application", Short: "Add a command to a Cobra Application",
@ -47,24 +44,43 @@ Example: cobra add server -> resulting in a new cmd/server.go`,
er("add needs a name for the command") er("add needs a name for the command")
} }
var project *Project commandName := validateCmdName(args[0])
if packageName != "" {
project = NewProject(packageName) if packageName == "" {
} else { // derive packageName
}
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
er(err) 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]) err = command.Create()
cmdPath := filepath.Join(project.CmdPath(), cmdName+".go") if err != nil {
createCmdFile(project.License(), cmdPath, cmdName) 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. // validateCmdName returns source without any dashes and underscore.
// If there will be dash or underscore, next letter will be uppered. // If there will be dash or underscore, next letter will be uppered.
@ -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 == "" {