From 02a0b7db0c784ee7c69d1272113b0e122ac069d1 Mon Sep 17 00:00:00 2001 From: Ian Walter Date: Tue, 3 May 2016 22:09:50 -0400 Subject: [PATCH 1/2] Fixing addCmd templating and finishing #266 --- cobra/cmd/add.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index 09c32c17..88257758 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -55,6 +55,8 @@ Example: cobra add server -> resulting in a new cmd/server.go for i := len(cmdParts) - 1; i >= 0; i-- { if i > 0 { pName = cmdParts[i-1] + } else { + pName = "RootCmd" } createCmdFile(cmdParts[i], buildPath(cmdParts, i - 1)) } @@ -84,7 +86,8 @@ package {{ .packageName }} import ( "fmt" {{ if .importpath }} - "{{ .importpath }}"{{ end }} + "{{ .importpath }}" + {{ end -}} "github.com/spf13/cobra" ) @@ -105,10 +108,14 @@ to quickly create a Cobra application.` + "`" + `, } func init() { - {{ if eq .parentName "RootCmd" }}{{ .parentName }}.AddCommand({{ .cmdName | title }}Cmd){{ end }} - {{ $cmdName := .cmdName }}{{ range $child := .children }} - {{ $cmdName | title }}Cmd.AddCommand({{ $cmdName }}.{{ $child | title }}Cmd){{ end }} + {{ 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. // Cobra supports Persistent Flags which will work for this command @@ -125,7 +132,7 @@ func init() { data["packageName"] = "cmd" - // + // Determine the package name based on the command file path. if cmdPath != "" { data["packageName"] = path.Base(cmdPath) } @@ -157,11 +164,6 @@ func init() { er(err) } 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 { fmt.Println(cmdName, "already exists at", path) } From 7e288525f01b5a4b2eb158a279954a84f14fcbc2 Mon Sep 17 00:00:00 2001 From: Ian Walter Date: Tue, 3 May 2016 22:16:18 -0400 Subject: [PATCH 2/2] Making buildPath arg more descriptive --- cobra/cmd/helpers.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go index 9d025c58..d2d0bb18 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/cmd/helpers.go @@ -352,9 +352,9 @@ func commentifyString(in string) string { return strings.Join(newlines, "\n") } -func buildPath(parts []string, end int) string { - if end > -1 { - return filepath.Join(buildPath(parts, end - 1), parts[end]) +func buildPath(parts []string, endIndex int) string { + if endIndex > -1 { + return filepath.Join(buildPath(parts, endIndex - 1), parts[endIndex]) } return "" }