Skip to content

Commit

Permalink
vgo - fixing up the add op to work with vgo
Browse files Browse the repository at this point in the history
  • Loading branch information
jharshman authored and spf13 committed Jun 7, 2019
1 parent 3741457 commit c7ac101
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 89 deletions.
129 changes: 43 additions & 86 deletions cobra/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, parentName string
var (
packageName string
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).
Expand All @@ -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(),
},
}

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.
Expand Down Expand Up @@ -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)
}
}
12 changes: 10 additions & 2 deletions cobra/cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type Project struct {
Legal License
Viper bool
AppName string
CmdName string
CmdParent string

// v1
absPath string
Expand All @@ -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
Expand Down Expand Up @@ -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 == "" {
Expand Down
2 changes: 1 addition & 1 deletion cobra/tpl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,4 @@ func init() {
// {{ .CmdName }}Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
`)
}
}

0 comments on commit c7ac101

Please sign in to comment.