Skip to content

Commit e1ee704

Browse files
committed
feat: add custom template UI interface
1 parent 7c4712a commit e1ee704

14 files changed

+72
-26
lines changed

cmd/sponge/server/handler.go

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ func GenerateCode(c *gin.Context) {
128128
handleGenerateCode(c, form.Path, form.Arg)
129129
}
130130

131+
// GetTemplateInfo get template info
132+
func GetTemplateInfo(c *gin.Context) {
133+
form := &GenerateCodeForm{}
134+
err := c.ShouldBindJSON(form)
135+
if err != nil {
136+
responseErr(c, err, errcode.InvalidParams)
137+
return
138+
}
139+
140+
handleGenerateCode(c, form.Path, form.Arg)
141+
}
142+
131143
// nolint
132144
func handleGenerateCode(c *gin.Context, outPath string, arg string) {
133145
out := "-" + time.Now().Format("150405")
@@ -157,11 +169,26 @@ func handleGenerateCode(c *gin.Context, outPath string, arg string) {
157169

158170
ctx, _ := context.WithTimeout(context.Background(), time.Minute*2) // nolint
159171
result := gobash.Run(ctx, "sponge", args...)
172+
resultInfo := ""
173+
count := 0
160174
for v := range result.StdOut {
161-
_ = v
175+
count++
176+
if count == 1 { // first line is the command
177+
continue
178+
}
179+
resultInfo += v
162180
}
163181
if result.Err != nil {
164-
responseErr(c, result.Err, errcode.InternalServerError)
182+
if params.OnlyPrint {
183+
response.Out(c, errcode.InternalServerError.RewriteMsg(result.Err.Error()))
184+
} else {
185+
responseErr(c, result.Err, errcode.InternalServerError)
186+
}
187+
return
188+
}
189+
190+
if params.OnlyPrint {
191+
response.Success(c, resultInfo)
165192
return
166193
}
167194

@@ -178,7 +205,8 @@ func handleGenerateCode(c *gin.Context, outPath string, arg string) {
178205
return
179206
}
180207

181-
c.Writer.Header().Set("content-disposition", gofile.GetFilename(zipFile))
208+
c.Writer.Header().Set("Content-Type", "application/zip")
209+
c.Writer.Header().Set("Content-Disposition", gofile.GetFilename(zipFile))
182210
c.File(zipFile)
183211

184212
recordObj().set(c.ClientIP(), outPath, params)
@@ -268,10 +296,10 @@ func UploadFiles(c *gin.Context) {
268296
for _, file := range files {
269297
filename := filepath.Base(file.Filename)
270298
fileType = path.Ext(filename)
271-
if !checkFileType(fileType) {
272-
response.Error(c, errcode.InvalidParams.RewriteMsg("only .proto or yaml files are allowed to be uploaded"))
273-
return
274-
}
299+
//if !checkFileType(fileType) {
300+
// response.Error(c, errcode.InvalidParams.RewriteMsg("only .proto or yaml files are allowed to be uploaded"))
301+
// return
302+
//}
275303

276304
filePath = savePath + "/" + filename
277305
if checkSameFile(hadSaveFiles, filePath) {
@@ -302,14 +330,14 @@ func UploadFiles(c *gin.Context) {
302330
// return valueSlice[0], nil
303331
//}
304332

305-
func checkFileType(typeName string) bool {
306-
switch typeName {
307-
case ".proto", ".yml", ".yaml":
308-
return true
309-
}
310-
311-
return false
312-
}
333+
//func checkFileType(typeName string) bool {
334+
// switch typeName {
335+
// case ".proto", ".yml", ".yaml", "json":
336+
// return true
337+
// }
338+
//
339+
// return false
340+
//}
313341

314342
func checkSameFile(files []string, file string) bool {
315343
for _, v := range files {
@@ -325,7 +353,7 @@ func getSavePath() string {
325353
if gofile.IsWindows() {
326354
dir = strings.ReplaceAll(saveDir, "\\", "/")
327355
}
328-
dir += "/" + krand.String(krand.R_All, 8)
356+
dir += "/" + "s_" + krand.String(krand.R_NUM|krand.R_LOWER, 10)
329357
if _, err := os.Stat(dir); os.IsNotExist(err) {
330358
_ = os.MkdirAll(dir, 0766)
331359
}

cmd/sponge/server/http.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func NewRouter(spongeAddr string, isLog bool) *gin.Engine {
5959

6060
apiV1 := r.Group("/api/v1")
6161
apiV1.POST("/generate", GenerateCode)
62+
apiV1.POST("/getTemplateInfo", GetTemplateInfo)
6263
apiV1.POST("/uploadFiles", UploadFiles)
6364
apiV1.POST("/listTables", ListTables)
6465
apiV1.GET("/listDrivers", ListDbDrivers)

cmd/sponge/server/record.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ type parameters struct {
3232
IncludeInitDB bool `json:"includeInitDB"`
3333
UpdateAt string `json:"updateAt"`
3434

35+
TemplateDir string `json:"templateDir"`
36+
Fields string `json:"fields"`
37+
DepProtoDir string `json:"depProtoDir"`
38+
OnlyPrint bool `json:"onlyPrint"`
39+
3540
SuitedMonoRepo bool `json:"suitedMonoRepo"`
3641
}
3742

@@ -101,6 +106,7 @@ func (r *record) get(ip string, commandType string) *parameters {
101106
return r.HostRecord[key]
102107
}
103108

109+
// nolint
104110
func parseCommandArgs(args []string) *parameters {
105111
var params = &parameters{UpdateAt: time.Now().Format("20060102T150405")}
106112
for _, v := range args {
@@ -112,6 +118,9 @@ func parseCommandArgs(args []string) *parameters {
112118
if ss[0] == "--include-init-db" {
113119
params.IncludeInitDB = true
114120
}
121+
if ss[0] == "--only-print" {
122+
params.OnlyPrint = true
123+
}
115124
} else {
116125
val := ss[1]
117126
switch ss[0] {
@@ -148,8 +157,16 @@ func parseCommandArgs(args []string) *parameters {
148157
params.ProtobufFile = val
149158
case "--yaml-file":
150159
params.YamlFile = val
160+
case "--tpl-dir":
161+
params.TemplateDir = val
162+
case "--fields":
163+
params.Fields = val
164+
case "--only-print":
165+
params.OnlyPrint = val == "true"
166+
case "--dep-proto-dir":
167+
params.DepProtoDir = val
151168
case "--suited-mono-repo":
152-
params.SuitedMonoRepo = (val == "true")
169+
params.SuitedMonoRepo = val == "true"
153170
}
154171
}
155172
}

cmd/sponge/server/static/css/app.c3da9907ea7c405547214b4bb7d5197e.css

Lines changed: 0 additions & 2 deletions
This file was deleted.

cmd/sponge/server/static/css/app.c3da9907ea7c405547214b4bb7d5197e.css.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

cmd/sponge/server/static/css/app.d86d99ed9313579ae97df6cbaf372030.css

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/sponge/server/static/css/app.d86d99ed9313579ae97df6cbaf372030.css.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Loading

cmd/sponge/server/static/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Sponge Generate Code</title><link rel=icon type=image/png sizes=32x32 href="/static/favicon.png?v=1.0"><script type=text/javascript src=/static/appConfig.js async></script><link href=/static/css/app.c3da9907ea7c405547214b4bb7d5197e.css rel=stylesheet></head><body style="margin: 0px; padding: 0px;"><style>.el-tooltip__popper {box-shadow: 3px 3px 10px 5px #d3d3d6;border-width: 0px !important;}
2-
.el-tooltip__popper[x-placement^="top"] .popper__arrow:after {border-top-color: #dcdfe6 !important;}</style><div id=app></div><script type=text/javascript src=/static/js/manifest.2ae2e69a05c33dfc65f8.js></script><script type=text/javascript src=/static/js/vendor.d8cdb3748af43d2ae51a.js></script><script type=text/javascript src=/static/js/app.545f8e7ca04ede9067fa.js></script></body></html>
1+
<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Sponge Generate Code</title><link rel=icon type=image/png sizes=32x32 href="/static/favicon.png?v=1.0"><script type=text/javascript src=/static/appConfig.js async></script><link href=/static/css/app.d86d99ed9313579ae97df6cbaf372030.css rel=stylesheet></head><body style="margin: 0px; padding: 0px;"><style>.el-tooltip__popper {box-shadow: 3px 3px 10px 5px #d3d3d6;border-width: 0px !important;}
2+
.el-tooltip__popper[x-placement^="top"] .popper__arrow:after {border-top-color: #dcdfe6 !important;}</style><div id=app></div><script type=text/javascript src=/static/js/manifest.2ae2e69a05c33dfc65f8.js></script><script type=text/javascript src=/static/js/vendor.d8cdb3748af43d2ae51a.js></script><script type=text/javascript src=/static/js/app.eb5d32169a92e7e955d4.js></script></body></html>

cmd/sponge/server/static/js/app.545f8e7ca04ede9067fa.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)