/
router_api.go
73 lines (60 loc) · 1.65 KB
/
router_api.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
package generate
import (
"context"
"fmt"
"strings"
"github.com/gin-admin/gin-admin-cli/v5/util"
)
func getRouterAPIFileName(dir string) string {
fullname := fmt.Sprintf("%s/internal/app/router/router.go", dir)
return fullname
}
func insertRouterAPI(ctx context.Context, dir, name string, excludeStatus, excludeCreate bool) error {
fullname := getRouterAPIFileName(dir)
pname := util.ToPlural(util.ToLowerUnderlinedNamer(name))
pname = strings.Replace(pname, "_", "-", -1)
injectContent, err := execParseTpl(routerAPITpl, map[string]interface{}{
"Name": name,
"PluralName": pname,
"IncludeStatus": !excludeStatus,
"IncludeCreate": !excludeCreate,
})
if err != nil {
return err
}
injectStart := 0
insertFn := func(line string) (data string, flag int, ok bool) {
if injectStart == 0 && strings.Contains(line, "v1 := g.Group") {
injectStart = 1
return
}
if injectStart == 1 && strings.Contains(line, "} // v1 end") {
injectStart = -1
data = injectContent.String()
flag = -1
ok = true
return
}
return "", 0, false
}
err = insertContent(fullname, insertFn)
if err != nil {
return err
}
fmt.Printf("File write success: %s\n", fullname)
return execGoFmt(fullname)
}
const routerAPITpl = `
g{{.Name}} := v1.Group("{{.PluralName}}")
{
g{{.Name}}.GET("", a.{{.Name}}API.Query)
g{{.Name}}.GET(":id", a.{{.Name}}API.Get)
g{{.Name}}.POST("", a.{{.Name}}API.Create)
g{{.Name}}.PUT(":id", a.{{.Name}}API.Update)
g{{.Name}}.DELETE(":id", a.{{.Name}}API.Delete)
{{if .IncludeStatus}}
g{{.Name}}.PATCH(":id/enable", a.{{.Name}}API.Enable)
g{{.Name}}.PATCH(":id/disable", a.{{.Name}}API.Disable)
{{end}}
}
`