-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (69 loc) · 1.54 KB
/
main.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 main
import (
"github.com/gogoclouds/gen/pkg"
"log"
"path/filepath"
"strings"
"text/template"
)
func main() {
var module = struct {
Project string // github.com/gogoclouds/gen
Struct string // User
// 可选
Path string // ./internal
ApiVersion string // v1
File string // user
Model string // sql table
}{
Path: "./internal",
Project: "github.com/gogoclouds/gen",
Struct: "User",
Model: "SysUser",
}
if module.ApiVersion == "" {
module.ApiVersion = "v1"
}
if module.File == "" {
module.File = strings.ToLower(module.Struct)
}
if module.Model == "" {
module.Model = module.Struct
}
filepaths := []string{
"./internal/tmpl/mvc/router.tmpl",
"./internal/tmpl/mvc/api.tmpl",
"./internal/tmpl/mvc/server.tmpl",
"./internal/tmpl/mvc/service.tmpl",
"./internal/tmpl/mvc/repo.tmpl",
}
for _, fp := range filepaths {
tp, err := template.ParseFiles(fp)
if err != nil {
log.Println(err)
return
}
filename := module.File + ".go"
dir := filepath.Join(module.Path, module.File) // ./internal/user
fType := strings.ToLower(strings.TrimSuffix(filepath.Base(fp), filepath.Ext(fp)))
switch fType {
case "router":
filename = "router.go"
case "api":
dir = filepath.Join(dir, fType, module.ApiVersion)
case "server":
dir = filepath.Join(dir, "handler")
default:
dir = filepath.Join(dir, fType)
}
f, err := pkg.MustOpen(filename, dir)
if err != nil {
panic(err)
}
err = tp.Execute(f, module)
f.Close()
if err != nil {
log.Println(err)
}
}
}