forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
188 lines (172 loc) · 4.25 KB
/
templates.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package tpl
import (
"fmt"
"path/filepath"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/core/errorx"
"github.com/zeromicro/go-zero/tools/goctl/api/apigen"
"github.com/zeromicro/go-zero/tools/goctl/api/gogen"
apinew "github.com/zeromicro/go-zero/tools/goctl/api/new"
"github.com/zeromicro/go-zero/tools/goctl/docker"
"github.com/zeromicro/go-zero/tools/goctl/kube"
mongogen "github.com/zeromicro/go-zero/tools/goctl/model/mongo/generate"
modelgen "github.com/zeromicro/go-zero/tools/goctl/model/sql/gen"
rpcgen "github.com/zeromicro/go-zero/tools/goctl/rpc/generator"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
const templateParentPath = "/"
// genTemplates writes the latest template text into file which is not exists
func genTemplates(_ *cobra.Command, _ []string) error {
path := varStringHome
if len(path) != 0 {
pathx.RegisterGoctlHome(path)
}
if err := errorx.Chain(
func() error {
return gogen.GenTemplates()
},
func() error {
return modelgen.GenTemplates()
},
func() error {
return rpcgen.GenTemplates()
},
func() error {
return docker.GenTemplates()
},
func() error {
return kube.GenTemplates()
},
func() error {
return mongogen.Templates()
},
func() error {
return apigen.GenTemplates()
},
func() error {
return apinew.GenTemplates()
},
); err != nil {
return err
}
dir, err := pathx.GetTemplateDir(templateParentPath)
if err != nil {
return err
}
abs, err := filepath.Abs(dir)
if err != nil {
return err
}
fmt.Printf("Templates are generated in %s, %s\n", aurora.Green(abs),
aurora.Red("edit on your risk!"))
return nil
}
// cleanTemplates deletes all templates
func cleanTemplates(_ *cobra.Command, _ []string) error {
path := varStringHome
if len(path) != 0 {
pathx.RegisterGoctlHome(path)
}
err := errorx.Chain(
func() error {
return gogen.Clean()
},
func() error {
return modelgen.Clean()
},
func() error {
return rpcgen.Clean()
},
func() error {
return docker.Clean()
},
func() error {
return kube.Clean()
},
func() error {
return mongogen.Clean()
},
func() error {
return apigen.Clean()
},
func() error {
return apinew.Clean()
},
)
if err != nil {
return err
}
fmt.Printf("%s\n", aurora.Green("templates are cleaned!"))
return nil
}
// updateTemplates writes the latest template text into file,
// it will delete the older templates if there are exists
func updateTemplates(_ *cobra.Command, _ []string) (err error) {
path := varStringHome
category := varStringCategory
if len(path) != 0 {
pathx.RegisterGoctlHome(path)
}
defer func() {
if err == nil {
fmt.Println(aurora.Green(fmt.Sprintf("%s template are update!", category)).String())
}
}()
switch category {
case docker.Category():
return docker.Update()
case gogen.Category():
return gogen.Update()
case kube.Category():
return kube.Update()
case rpcgen.Category():
return rpcgen.Update()
case modelgen.Category():
return modelgen.Update()
case mongogen.Category():
return mongogen.Update()
case apigen.Category():
return apigen.Update()
case apinew.Category():
return apinew.Update()
default:
err = fmt.Errorf("unexpected category: %s", category)
return
}
}
// revertTemplates will overwrite the old template content with the new template
func revertTemplates(_ *cobra.Command, _ []string) (err error) {
path := varStringHome
category := varStringCategory
filename := varStringName
if len(path) != 0 {
pathx.RegisterGoctlHome(path)
}
defer func() {
if err == nil {
fmt.Println(aurora.Green(fmt.Sprintf("%s template are reverted!", filename)).String())
}
}()
switch category {
case docker.Category():
return docker.RevertTemplate(filename)
case kube.Category():
return kube.RevertTemplate(filename)
case gogen.Category():
return gogen.RevertTemplate(filename)
case rpcgen.Category():
return rpcgen.RevertTemplate(filename)
case modelgen.Category():
return modelgen.RevertTemplate(filename)
case mongogen.Category():
return mongogen.RevertTemplate(filename)
case apigen.Category():
return apigen.RevertTemplate(filename)
case apinew.Category():
return apinew.RevertTemplate(filename)
default:
err = fmt.Errorf("unexpected category: %s", category)
return
}
}