mirror of
https://github.com/spf13/cobra
synced 2024-11-25 07:07:15 +00:00
Reversing cmd creation order so child cmds get added properly
This commit is contained in:
parent
274ca840ae
commit
784ab4d6eb
2 changed files with 54 additions and 21 deletions
|
@ -14,7 +14,6 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path"
|
"path"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -52,22 +51,13 @@ Example: cobra add server -> resulting in a new cmd/server.go
|
||||||
guessProjectPath()
|
guessProjectPath()
|
||||||
|
|
||||||
// Iterate over command parts delimited by "/".
|
// Iterate over command parts delimited by "/".
|
||||||
currentPath := ""
|
|
||||||
cmdParts := strings.Split(args[0], "/")
|
cmdParts := strings.Split(args[0], "/")
|
||||||
for n, cmd := range cmdParts {
|
for i := len(cmdParts) - 1; i >= 0; i-- {
|
||||||
//
|
if i > 0 {
|
||||||
if n > 0 {
|
pName = cmdParts[i-1]
|
||||||
// Set parent name to previous command's name.
|
|
||||||
pName = cmdParts[n-1]
|
|
||||||
|
|
||||||
//
|
|
||||||
currentPath += pName + "/"
|
|
||||||
}
|
}
|
||||||
|
createCmdFile(cmdParts[i], buildPath(cmdParts, i - 1))
|
||||||
createCmdFile(cmd, currentPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,7 +83,8 @@ package {{ .packageName }}
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
{{ if .importpath }}
|
||||||
|
"{{ .importpath }}"{{ end }}
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -114,18 +105,19 @@ to quickly create a Cobra application.` + "`" + `,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
{{ .parentName | title }}.AddCommand({{ .cmdName | title }}Cmd)
|
{{ if eq .parentName "RootCmd" }}{{ .parentName }}.AddCommand({{ .cmdName | title }}Cmd){{ end }}
|
||||||
|
{{ $cmdName := .cmdName }}{{ range $child := .children }}
|
||||||
|
{{ $cmdName | title }}Cmd.AddCommand({{ $cmdName }}.{{ $child | title }}Cmd){{ end }}
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
// and all subcommands, e.g.:
|
// and all subcommands, e.g.:
|
||||||
// {{.cmdName}}Cmd.PersistentFlags().String("foo", "", "A help for foo")
|
// {{ .cmdName | title }}Cmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
// Cobra supports local flags which will only run when this command
|
// Cobra supports local flags which will only run when this command
|
||||||
// is called directly, e.g.:
|
// is called directly, e.g.:
|
||||||
// {{.cmdName}}Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
// {{ .cmdName | title }}Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
@ -148,12 +140,28 @@ func init() {
|
||||||
path := filepath.Join(ProjectPath(), guessCmdDir(), cmdPath)
|
path := filepath.Join(ProjectPath(), guessCmdDir(), cmdPath)
|
||||||
filename := cmdName + ".go"
|
filename := cmdName + ".go"
|
||||||
|
|
||||||
|
children := getChildNames(path, cmdName)
|
||||||
|
if len(children) > 0 {
|
||||||
|
data["importpath"] = filepath.Join(
|
||||||
|
guessImportPath(),
|
||||||
|
guessCmdDir(),
|
||||||
|
cmdPath,
|
||||||
|
cmdName,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
data["children"] = children
|
||||||
|
|
||||||
// Check if the file exists before trying to create it.
|
// Check if the file exists before trying to create it.
|
||||||
if _, err := os.Stat(filepath.Join(path, filename)); os.IsNotExist(err) {
|
if doesExist, err := exists(filepath.Join(path, filename)); !doesExist {
|
||||||
if err = writeTemplateToFile(path, filename, template, data); err != nil {
|
if err = writeTemplateToFile(path, filename, template, data); err != nil {
|
||||||
er(err)
|
er(err)
|
||||||
}
|
}
|
||||||
fmt.Println(cmdName, "created at", path)
|
fmt.Println(cmdName, "created at", path)
|
||||||
|
} else if len(children) > 0 {
|
||||||
|
if err = writeTemplateToFile(path, filename, template, data); err != nil {
|
||||||
|
er(err)
|
||||||
|
}
|
||||||
|
fmt.Println(cmdName, "updated at", path)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(cmdName, "already exists at", path)
|
fmt.Println(cmdName, "already exists at", path)
|
||||||
}
|
}
|
||||||
|
|
|
@ -248,7 +248,10 @@ func writeStringToFile(path, file, text string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func templateToReader(tpl string, data interface{}) (io.Reader, error) {
|
func templateToReader(tpl string, data interface{}) (io.Reader, error) {
|
||||||
tmpl := template.New("").Funcs(template.FuncMap{"title": strings.Title})
|
tmpl := template.New("").Funcs(template.FuncMap{
|
||||||
|
"title": strings.Title,
|
||||||
|
"eq": func(a, b interface{}) bool { return a == b },
|
||||||
|
})
|
||||||
tmpl.Funcs(funcMap)
|
tmpl.Funcs(funcMap)
|
||||||
tmpl, err := tmpl.Parse(tpl)
|
tmpl, err := tmpl.Parse(tpl)
|
||||||
|
|
||||||
|
@ -348,3 +351,25 @@ func commentifyString(in string) string {
|
||||||
}
|
}
|
||||||
return strings.Join(newlines, "\n")
|
return strings.Join(newlines, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildPath(parts []string, end int) string {
|
||||||
|
if end > -1 {
|
||||||
|
return filepath.Join(buildPath(parts, end - 1), parts[end])
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func getChildNames(path, name string) []string {
|
||||||
|
childNames := []string{}
|
||||||
|
fileNames := []string{}
|
||||||
|
if file, err := os.Open(filepath.Join(path, name)); err == nil {
|
||||||
|
fileNames, err = file.Readdirnames(0)
|
||||||
|
}
|
||||||
|
for _, name := range fileNames {
|
||||||
|
// Only consider files with the .go extension to be child commands.
|
||||||
|
if ext := filepath.Ext(name); ext == ".go" {
|
||||||
|
childNames = append(childNames, strings.TrimSuffix(name, ext))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return childNames
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue