-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
341 lines (266 loc) · 10.4 KB
/
Makefile
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#版本号
#branch = test
#当前项目目录名字
dirName := $(shell basename $(PWD))
envPath = cs_healthy/$(dirName)/$(env)
dockerName = $(dirName)-$(env)
currentBranch = $(shell git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
find_str = "github_com_yituoshiniao_gin-api-http_"
replace_str = ""
swag_path = "gen/swag-doc"
swagger_file = "/$(dirName)/$(swag_path)/swagger/swagger.json"
api_html_file = "/$(dirName)/gen/html2/index.html"
api_tmp_html_file = "/$(dirName)/gen/html2/api.html"
SRCS = $(shell git ls-files '*.go')
#docker hub 仓库地址
docker_hub_url = "hub.docker.com"
pwd:
@echo $(dirName)
#branch =v$(shell (date "+%Y.%m.%d.%H.%M.%S"))
#branch =v$(shell (date "+%Y.%m.%d.%H.%M"))
#运行一下所有命令
all:wire db doc
#一键生成doc快捷命令; [1、生成swag; 2、替换多余字符串;]
doc: doc-swag replace-swag-json
#生成openapi sdk 客户端
gen-openapi:openapi-doc-docker-go openapi-doc-docker-lua openapi-doc-html openapi-documentation openapi-doc-cwiki gen-subsplit
demo:
echo $(branch)
echo $(envPath)
echo $(dockerName)
echo $(docker_uname)
echo $(docker_pwd)
#测试环境pull + 登录 docker login
docker_uname = 'robot$$cs_healthy+jenkins'
docker_pwd = "YToo5noykvHFrjDUnSCoe6onWoqSXI0A"
pull-image-v2:
docker login -u '$(docker_uname)' -p $(docker_pwd) $(docker_hub_url) \
docker pull $(docker_hub_url)/$(envPath):$(branch)
@echo "拉取成功版本:"$(branch)
#构建linux环境包
build:
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o app cmd/server/main.go
#构建mac 环境包
build-mac:
go build -o app cmd/server/main.go
#test 构建项目 创建镜像image
build-image:
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o app cmd/server/main.go;\
docker login -u $(docker_uname) -p $(docker_pwd) $(docker_hub_url); \
docker build -t $(docker_hub_url)/$(envPath):$(branch) -f Dockerfile . ; \
rm app
@echo "构建成功版本:"$(branch)
login:
echo '$(docker_uname)'
echo "$(docker_uname)"
echo $(docker_uname)
docker login -u $(docker_uname) -p $(docker_pwd) $(hub.docker.com)
#拉取镜像image到本地
pull-image:
docker login -u $(docker_uname) -p $(docker_pwd) $(docker_hub_url) \
docker pull $(docker_hub_url)/$(envPath):$(branch)
@echo "拉取成功版本:"$(branch)
#推送镜像image到hub
push-image:
docker push $(docker_hub_url)/$(envPath):$(branch)
@echo "推送成功版本:"$(branch)
#1、构建镜像; 2 推送镜像到hub
test: build-image push-image
#日志上报配置详见: https://doc.abc.net/pages/viewpage.action?pageId=350650880
#运行
run-local:
docker stop $(dockerName); \
docker rm $(dockerName); \
docker run --name $(dockerName) \
-e envPath=$(env) \
-d -p 3013:3013 -p 6013:6013 \
--log-driver=syslog \
--log-opt syslog-address=udp://10.2.8.203:10058 \
--restart always $(docker_hub_url)/$(envPath):$(branch)
@echo "启动成功 容器名:"$(dockerName)
#启动gateway
deploy: run-local
##********生成swagger文档 ****##
#merge 有三种导入方式(v1.3.23+支持) normal, good, mergin
#普通模式(normal):不导入已存在的接口;
#智能合并(good):已存在的接口,将合并返回数据的 response,适用于导入了 swagger 数据,保留对数据结构的改动;
#完全覆盖(mergin):不保留旧数据,完全使用新数据,适用于接口定义完全交给后端定义, 默认为 normal
#--parseDependency --parseInternal外部依赖struct解析 https://blog.csdn.net/lsjweiyi/article/details/122948171
#parseDepth 解析依赖深度, 默认100;
#swag swag-doc 文档参数: https://blog.csdn.net/qq_41630102/article/details/128411210
#--tags="iap_apple" 生成那些tag的文档,多个用逗号[,]分割
# --generatedTime 是否在 docs.go 顶部生成时间戳,默认 false
doc-swag:
#swag init -g cmd/server/main.go -o gen/swag-doc/swagger
swag fmt
swag init -md ./$(swag_path)/md \
--parseDependency \
--parseInternal=true \
--parseGoList=true \
--parseDepth=300 \
-g cmd/server/main.go \
--generatedTime=false \
-o $(swag_path)/swagger
sleep 1
#yapi-import-tmp.json yapi 服务器配置,其中server配置是服务端地址
yapi import || true
@echo "swagger 成功"
#copy-doc:
# rm -rf ./gen/swagger
# cp -rf ./$(swag_path)/swagger ./gen #拷贝swagger到gen目录下
# @echo "success 成功"
#替换 find_str 字符串为 replace_str 字符串
replace-swag-json:
$(shell (find ./$(swag_path)/swagger -iname "*.json") | xargs -n1 -IX bash -c 'sed s/$(find_str)/$(replace_str)/ X > X.tmp && mv X{.tmp,}')
$(shell (find ./$(swag_path)/swagger -iname "*.go") | xargs -n1 -IX bash -c 'sed s/$(find_str)/$(replace_str)/ X > X.tmp && mv X{.tmp,}')
$(shell (find ./$(swag_path)/swagger -iname "*.yaml") | xargs -n1 -IX bash -c 'sed s/$(find_str)/$(replace_str)/ X > X.tmp && mv X{.tmp,}')
@echo "swagger 成功"
#支持ssh地址,当不支持ssh地址可以使用http
#git subsplit publish --no-tags --heads=$(currentBranch) gen:git@gitlab.abc.net:cs-server2/services/openapi-client.git
#subtree 更新管理子分支
#详情:https://github.com/dflydev/git-subsplit
gen-subsplit:
@echo "当前git分支 $(currentBranch)"
rm -rf .subsplit
git subsplit init .
git subsplit publish --no-tags --heads=$(currentBranch) gen:https://github.com/yituoshiniao/openapi-client.git #不支持ssh,使用它http同步
git subsplit publish --no-tags --heads=$(currentBranch) gen/go:https://github.com/yituoshiniao/openapi-client-go.git
#git subsplit publish --no-tags --heads=$(currentBranch) gen/lua:https://github.com/yituoshiniao/openapi-client-lua.git
rm -rf .subsplit
@echo "执行成功"
# 生成 openapi-generator 生成多语言sdk服务端(本地命令)
#详情:https://openapi-generator.tech/docs/generators
openapi-doc-server:
rm -rf ./$(swag_path)/swagger/go
npx @openapitools/openapi-generator-cli generate -i ./$(swag_path)/swagger/swagger.json \
-g go-gin-server \
-o ./gen/go-gin-server
@echo "执行成功"
#生成 Confluence wiki 文档
out_path_cwiki = "/gen/cwiki"
openapi-doc-cwiki:
docker run --rm \
-v ${PWD}:/$(dirName) openapitools/openapi-generator-cli generate \
-i $(swagger_file) \
-g cwiki \
-o .$(out_path_cwiki)
@echo "执行成功"
#生成html文档 #-c "./$(swag_path)/swagger/config.json"
out_path_html = "/$(dirName)/gen/html2"
openapi-doc-html:
rm -rf .$(out_path_html)
docker run --rm \
-v ${PWD}:/$(dirName) openapitools/openapi-generator-cli generate \
-i $(swagger_file) \
-g html2 \
-o $(out_path_html)
@echo "执行成功"
openapi-documentation-openapi:
npx @openapitools/openapi-generator-cli generate \
-i ./$(swag_path)/swagger/swagger.json \
-g openapi-yaml \
-o ./gen/openapi-yaml
#同步markdown文档到sdk的client 中
out_path_md = "/$(dirName)/gen/markdown"
openapi-documentation:
docker run --rm \
-v ${PWD}:/$(dirName) openapitools/openapi-generator-cli generate \
-i $(swagger_file) \
-g markdown \
-o $(out_path_md)
cp -r ./gen/markdown/* ..$(out_lua_path) && cp -rf ..$(swagger_file) ..$(out_lua_path) && rm -rf ./gen/markdown
#复制 swagger.json 接口文档到 sdk client中
cp -rf ..$(swagger_file) ..$(out_go_path) && cp -rf ..$(swagger_file) ..$(out_go_path)
#复制html接口文档到 sdk client中
cp ..$(api_html_file) ..$(api_tmp_html_file)
cp -rf ..$(api_tmp_html_file) ..$(out_go_path) && cp -rf ..$(api_tmp_html_file) ..$(out_lua_path) && rm -rf ..$(api_tmp_html_file)
@echo "执行success"
# 生成 openapi-generator 生成多语言sdk客户端(本地命令)
openapi-doc:
rm -rf ./$(swag_path)/swagger/go
npx @openapitools/openapi-generator-cli generate \
-i ./$(swag_path)/swagger/swagger.json \
-g go \
-o ./api/swagger/go_bak
#git 仓库host
git-host = "github.com"
#git仓库host后的路径
git-user-id = "yituoshiniao"
##指定运行容器版本 -v ${PWD}:$(dirName) openapitools/openapi-generator-cli:v7.5.0 generate \
#通过docker生成 openapi-generator 生成多语言sdk客户端
# -i petstore.yaml 表示要根据哪个文档生成,支持yaml和json
# -g ruby 表示生成ruby client
# -o /tmp/test/ 指定存放的地址
#go 生成 openapi客户端路径
out_go_path = "/$(dirName)/gen/go"
openapi-doc-docker-go:
rm -rf ..$(out_go_path)
docker run --rm \
-v ${PWD}:/$(dirName) openapitools/openapi-generator-cli generate \
-i $(swagger_file) \
-g go \
--git-host $(git-host) \
--git-user-id $(git-user-id) \
--git-repo-id "openapi-client-go" \
--release-note "openapi生成go客户端demo" \
-o $(out_go_path)
#更新go mod
cd ..$(out_go_path) && go mod tidy
@echo "执行成功"
out_lua_path = "/$(dirName)/gen/lua"
openapi-doc-docker-lua:
rm -rf ..$(out_lua_path)
docker run --rm \
-v ${PWD}:/$(dirName) openapitools/openapi-generator-cli generate \
-i $(swagger_file) \
--git-host $(git-host) \
--git-user-id $(git-user-id) \
--git-repo-id "openapi-client-lua" \
--release-note "openapi生成-lua客户端demo" \
-g lua \
-o $(out_lua_path)
@echo "执行成功"
out_java_path = "/$(dirName)/gen/java"
openapi-doc-docker-java:
rm -rf ..$(out_java_path)
docker run --rm \
-v ${PWD}:/$(dirName) openapitools/openapi-generator-cli generate \
-i $(swagger_file) \
--git-host $(git-host) \
--git-user-id $(git-user-id) \
--git-repo-id "openapi-client-java" \
--release-note "openapi生成-java客户端demo" \
-g java \
-o $(out_java_path)
@echo "执行成功"
#拉取代码
#make git-pull env=test branch=demo
#构建镜像
#make build-image env=test branch=demo
#推送镜像
#make push-image env=test branch=demo
#部署镜像
#make deploy env=test branch=demo
#gentool使用doc:https://github.com/go-gorm/gen/tree/master/tools/gentool
#安装 gentool: go install gorm.io/gen/tools/gentool@latest
#tables 指定生成的表,空则全部表生成
#onlyModel 只生成 model模型,不生成查询query
# -outPath "dao/a" 指定生产文件目录,默认 更项目的dao目录
#-tables 指定生成的表明,生成全部 则表留空即可
# -withUnitTest 为查询代码生成单元测试
#生成gorm运行文件
gentool:
gentool -dsn "root:passw0rd@tcp(127.0.0.1:3306)/db_goods_center?charset=utf8mb4&parseTime=True&loc=Local" \
-tables "apple_product_price,apple_config,user_score" \
-fieldWithTypeTag \
-fieldWithIndexTag \
-modelPkgName "model"
#自定义 gorm Generator 生成db文件
db:
go run cmd/gen/main.go
#wire执行
wire:
cd ./inject/ && wire
## Format the Code
gofmt:
gofmt -s -l -w $(SRCS)