-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmpl.go
105 lines (91 loc) · 2.3 KB
/
tmpl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"bytes"
"text/template"
)
var rootTmpl = `package {{ .PkgName }}
{{ $r := .RootPkgName }}
import (
"fmt"
"github.com/spf13/cobra"
{{ if ne .PkgName .ParentPkg}}
{{ if .Cmd.Sub}}
{{ range .Cmd.Sub }}
{{ if .Sub}}
"{{ $r }}/{{ .File.PkgPath }}"
{{ end }}
{{ end }}
{{ end }}
{{ end }}
)
{{ $base := .Cmd }}
var {{ if ne .Cmd.FuncName "Cmd"}}{{ .Cmd.FuncName }}{{ end }}Cmd = &cobra.Command{
Use: "{{ .Cmd.Name }}{{range $arg := .Cmd.Args}} [{{ $arg }}]{{ end }}",
Short: "{{ .Cmd.Name }} ...",
{{ if .Cmd.Args }}
Args: cobra.MatchAll(cobra.ExactArgs({{ len .Cmd.Args }}), cobra.OnlyValidArgs),
{{ end }}
Run: func(cmd *cobra.Command, args []string) {
{{if .Cmd.Args }}
{{ range $k, $arg := .Cmd.Args }}
{{ $arg }} := args[{{ $k }}]
fmt.Printf("{{ $arg }}: %v\n", {{ $arg }})
{{ end }}
{{ end }}
// TODO: Implement command
},
}
{{$pkg := .PkgName}}
{{$parent := .ParentPkg}}
{{$cmd := .Cmd}}
{{if $cmd.Flags}}
{{ range $key, $flag := $cmd.Flags }}
var {{ $cmd.FuncName }}{{index $cmd.FlagsPretty $key}} string
{{ end }}
{{ end }}
func init() {
// Flags
{{ if .Cmd.Flags}}
{{ range $key, $flag := .Cmd.Flags }}
// {{ $key }} : {{ $flag }}
{{ if eq $pkg $parent }}{{ if $cmd }}{{ $cmd.FuncName }}Cmd.Flags().StringVarP(&{{ $cmd.FuncName }}{{index $cmd.FlagsPretty $key}}, "{{ $key }}", "", "", "desc"){{end}}
{{ else }}
{{ if $cmd }}{{ if ne $cmd.FuncName "Cmd"}}{{ $cmd.FuncName }}{{ end }}Cmd.PersistentFlags().StringVarP(&{{ $cmd.FuncName }}{{index $cmd.FlagsPretty $key}}, "{{ $key }}", "", "", "desc") {{end}}
{{end}}
{{ end }}
{{ end }}
{{ if eq .PkgName .ParentPkg}}{{ if .Cmd.Parent }}{{ .Cmd.Parent.FuncName }}Cmd.AddCommand({{ .Cmd.FuncName }}Cmd)
{{ end }}{{ end }}
{{ if ne .PkgName .ParentPkg}}
{{ if .Cmd.Sub}}
{{ range .Cmd.Sub }}
{{ if .Sub}}
{{ if ne $base.FuncName "Cmd"}}{{ $base.FuncName }}{{ end }}Cmd.AddCommand({{.File.PkgName}}.{{ .FuncName }}Cmd)
{{ end }}
{{ end }}
{{ end }}
{{ end }}
}
`
func exampleTmpl(mod string) string {
return `package main
import (
"` + mod + `/cmd"
)
func main(){
cmd.Cmd.Execute()
}
`
}
func execTmpl(f *File) []byte {
tmpl, err := template.New("main").Parse(rootTmpl)
if err != nil {
panic(err)
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, f)
if err != nil {
panic(err)
}
return buf.Bytes()
}