-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
194 lines (174 loc) · 4.94 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
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
189
190
191
192
193
194
package main
import (
"flag"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/gohouse/converter"
"github.com/gohouse/file"
"github.com/gohouse/gorose/v2"
"github.com/gohouse/goship/builder"
"github.com/gohouse/goship/config"
"github.com/gohouse/goship/templates"
"github.com/gohouse/goship/util"
"html/template"
"log"
"os"
)
var f string
var e string
func init() {
flag.StringVar(&f, "f", "", "file:运行需要指定的配置文件")
flag.StringVar(&e, "e", "", "export:导出配置文件模板")
flag.Parse()
if f == "" {
f = "./config.toml"
}
}
func main() {
// 检查导出配置
if e != "" {
checkExport(e)
return
}
// 检查依赖
log.Println("检查依赖 ...")
if !util.CheckCommandExists("go") {
log.Println("未检测到go,请先安装golang")
return
}
if !util.CheckCommandExists("swag") {
// 尝试安装 swag
log.Println("未检测到swag,正在尝试自动安装 swag : go get -u github.com/swaggo/swag/cmd/swag ...")
err := util.RunCmd("go get -u github.com/swaggo/swag/cmd/swag > /dev/null 2>&1")
if err!= nil {
log.Println("尝试安装 swag 失败,请手动安装gin-swagger,具体可以参考文档")
return
}
if !util.CheckCommandExists("swag") { // 如果依旧未安装成功,则提示用户手动安装
log.Println("未检测到swag,请先安装gin-swagger,具体可以参考文档")
return
}
log.Println("swag 安装成功 !")
}
// 解析配置
var c config.Config
config.ParseConfig(f, &c)
// 驱动数据库
engin, err := gorose.Open(&c.Database)
if err != nil {
panic(err.Error())
}
// 初始化项目
log.Println("初始化项目 ...")
projectInit(&c)
// 替换 module
log.Println("替换 module ...")
replaceModulePlaceholder(&c)
// 生成路由
log.Println("生成路由 ...")
builder.NewRouting(engin, &c).Build()
// 生成api
log.Println("生成api ...")
builder.NewApi(engin, &c).Build()
// 生成 model
log.Println("生成 model ...")
genModel(engin, &c)
// 写入数据库配置到配置文件
log.Println("写入数据库配置到配置文件 ...")
addDbConf(&c)
// 生成 swagger api 文档
log.Println("生成 swagger api 文档 ...")
if err = genSwag(&c); err != nil {
log.Println("生成swagger api失败:", err.Error())
return
}
// 生成 go.mod
log.Println("生成 go.mod ...")
if err = genGoMod(&c); err != nil {
log.Println("生成go.mod失败:", err.Error())
return
}
fmt.Println("\033[46;30m goship finish success !!! \033[0m")
}
func checkExport(e string) {
_, err := file.NewFile(e).Write(templates.GetConfigTemplate())
if err != nil {
panic(err.Error())
}
}
func projectInit(c *config.Config) {
util.RunCmd(fmt.Sprintf("rm -rf %s", c.SiteInfo.RootDir))
//util.RunCmd(fmt.Sprintf("cp -r ~/go/src/github.com/gohouse/goship-template %s", c.SiteInfo.RootDir))
util.RunCmd(fmt.Sprintf("git clone %s --depth=1 %s", c.SiteInfo.GoshipTemplate, c.SiteInfo.RootDir))
}
func replaceModulePlaceholder(c *config.Config) {
files, err := file.GetAllFiles(c.SiteInfo.RootDir)
if err != nil {
panic(err.Error())
}
for _, f := range files {
file.ReplaceFileContent(f, "goshipdemo", c.SiteInfo.GoModule)
}
}
func genGoMod(c *config.Config) error {
if c.SiteInfo.GoModule == "goshipdemo" {
return util.RunCmds([]string{
fmt.Sprintf("cd %s", c.SiteInfo.RootDir),
"go mod tidy",
})
}
var gomodfile = fmt.Sprintf("%s/go.mod", c.SiteInfo.RootDir)
if err := file.ReplaceFileContent(gomodfile, "goshipdemo", c.SiteInfo.GoModule); err != nil {
return err
}
return util.RunCmds([]string{
fmt.Sprintf("cd %s", c.SiteInfo.RootDir),
"go mod tidy",
})
}
func genModel(engin *gorose.Engin, c *config.Config) {
err := converter.NewTable2Struct().
Config(&converter.T2tConfig{StructNameToHump: true}).
SavePath(fmt.Sprintf("%s/%s/%s", c.SiteInfo.RootDir, c.Dir.Model, "model.go")).
DB(engin.GetQueryDB()).
TagKey("gorose").
RealNameMethod("TableName").
EnableJsonTag(true).
Prefix(engin.GetPrefix()).
Run()
if err != nil {
panic(err.Error())
}
}
func addDbConf(c *config.Config) {
// 初始化配置文件
var conffile_example = fmt.Sprintf("%s/config.toml.example", c.SiteInfo.RootDir)
var conffile = fmt.Sprintf("%s/config.toml", c.SiteInfo.RootDir)
err := util.RunCmd(fmt.Sprintf("cp %s %s", conffile_example, conffile))
if err != nil {
panic(err)
}
// 替换数据库配置
tmpl := template.New("conf")
_, err = tmpl.Parse(`
# 数据库配置
[database]
driver = "{{.Driver}}" # 数据库驱动
dsn = "{{.Dsn}}" # dsn链接
setMaxOpenConns = {{.SetMaxOpenConns}} # 连接池 - 最大打开连接数
setMaxIdleConns = {{.SetMaxIdleConns}} # 连接池 - 最大空闲连接数
prefix = "{{.Prefix}}" # 表前缀`)
if err != nil {
panic(err.Error())
}
err = tmpl.Execute(util.NewFileWithMod(conffile, os.O_APPEND|os.O_CREATE|os.O_WRONLY), c.Database)
if err != nil {
panic(err.Error())
}
}
func genSwag(c *config.Config) error {
return util.RunCmds([]string{
fmt.Sprintf("cd %s", c.SiteInfo.RootDir),
"swag init",
})
}