Merge branch 'master' of github.com:ianwalter/cobra

This commit is contained in:
Ian Walter 2016-05-09 15:30:18 -04:00
commit 36bbf32d1d
2 changed files with 15 additions and 13 deletions

View file

@ -55,6 +55,8 @@ Example: cobra add server -> resulting in a new cmd/server.go
for i := len(cmdParts) - 1; i >= 0; i-- { for i := len(cmdParts) - 1; i >= 0; i-- {
if i > 0 { if i > 0 {
pName = cmdParts[i-1] pName = cmdParts[i-1]
} else {
pName = "RootCmd"
} }
createCmdFile(cmdParts[i], buildPath(cmdParts, i - 1)) createCmdFile(cmdParts[i], buildPath(cmdParts, i - 1))
} }
@ -84,7 +86,8 @@ package {{ .packageName }}
import ( import (
"fmt" "fmt"
{{ if .importpath }} {{ if .importpath }}
"{{ .importpath }}"{{ end }} "{{ .importpath }}"
{{ end -}}
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -105,10 +108,14 @@ to quickly create a Cobra application.` + "`" + `,
} }
func init() { func init() {
{{ if eq .parentName "RootCmd" }}{{ .parentName }}.AddCommand({{ .cmdName | title }}Cmd){{ end }} {{ if eq .parentName "RootCmd" -}}
{{ $cmdName := .cmdName }}{{ range $child := .children }} {{ .parentName }}.AddCommand({{ .cmdName | title }}Cmd)
{{ $cmdName | title }}Cmd.AddCommand({{ $cmdName }}.{{ $child | title }}Cmd){{ end }} {{ 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
@ -125,7 +132,7 @@ func init() {
data["packageName"] = "cmd" data["packageName"] = "cmd"
// // Determine the package name based on the command file path.
if cmdPath != "" { if cmdPath != "" {
data["packageName"] = path.Base(cmdPath) data["packageName"] = path.Base(cmdPath)
} }
@ -157,11 +164,6 @@ func init() {
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)
} }

View file

@ -352,9 +352,9 @@ func commentifyString(in string) string {
return strings.Join(newlines, "\n") return strings.Join(newlines, "\n")
} }
func buildPath(parts []string, end int) string { func buildPath(parts []string, endIndex int) string {
if end > -1 { if endIndex > -1 {
return filepath.Join(buildPath(parts, end - 1), parts[end]) return filepath.Join(buildPath(parts, endIndex - 1), parts[endIndex])
} }
return "" return ""
} }