Skip to content

Commit 09840cc

Browse files
authoredMay 4, 2020
Merge pull request #4 from LyricTian/v6.0
Refactoring to gin-admin v6.0
2 parents 02ff765 + d043719 commit 09840cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+11379
-975
lines changed
 

‎README.md

Lines changed: 49 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ USAGE:
1515
gin-admin-cli new [command options] [arguments...]
1616

1717
OPTIONS:
18-
--dir value, -d value 项目生成目录
19-
--pkg value, -p value 项目包名
20-
--core, -c 使用core分支
21-
--mirror, -m 使用国内镜像(gitee.com)
22-
--web, -w 包含web项目
18+
--dir value, -d value 项目生成目录(默认GOPATH)
19+
--pkg value, -p value 项目包名
20+
--branch value, -b value 指定分支(默认master)
21+
--mirror, -m 使用国内镜像(gitee.com)
22+
--web, -w 包含web项目
2323
```
2424

2525
> 使用示例
2626
2727
```
28-
$ gin-admin-cli new -m -d ~/go/src/test-gin-admin -p test-gin-admin
28+
$ gin-admin-cli new -p test-gin-admin -m
2929
```
3030

3131
### 生成业务模块
@@ -37,104 +37,74 @@ USAGE:
3737
gin-admin-cli generate [command options] [arguments...]
3838

3939
OPTIONS:
40-
--dir value, -d value 项目生成目录
40+
--dir value, -d value 项目生成目录(默认GOPATH)
4141
--pkg value, -p value 项目包名
42-
--ctl value 控制器swagger模板(支持default(基于github.com/swaggo/swag)和tb(基于github.com/teambition/swaggo)) (default: "default")
43-
--router value 路由模块(routers/api/api.go) (default: "api")
4442
--name value, -n value 业务模块名称(结构体名称)
4543
--comment value, -c value 业务模块注释(结构体注释)
46-
--file value, -f value 指定模板文件(.json,模板配置可参考说明)
47-
--module value, -m value 指定生成模块(以逗号分隔,支持:all,schema,entity,model,bll,ctl,api)
44+
--file value, -f value 指定模板文件(.yaml,模板配置可参考说明)
45+
--module value, -m value 指定生成模块(默认生成全部模块,以逗号分隔,支持:schema,model,bll,api,mock,router)
46+
--storage value, -s value 指定model的实现存储(默认gorm,支持:mongo/gorm)
4847
```
4948

5049
> 使用示例
5150
5251
```bash
53-
$ gin-admin-cli g -d ./test-gin-admin -p test-gin-admin -n Task -c '任务管理'
52+
$ gin-admin-cli g -p test-gin-admin -n Task -c '任务管理'
5453
```
5554

5655
#### 指定配置文件生成模块
5756

5857
```bash
59-
$ gin-admin-cli g -d 项目目录 -p 包名 -f 配置文件(json)
58+
$ gin-admin-cli g -p 包名 -f 配置文件(yaml)
6059
```
6160

6261
> 配置文件说明
6362
64-
```json
65-
{
66-
"struct_name": "结构体名称",
67-
"comment": "结构体注释说明",
68-
"fields": [
69-
{
70-
"struct_field_name": "结构体字段名称",
71-
"comment": "结构体字段注释",
72-
"struct_field_required": "结构体字段是否是必选项",
73-
"struct_field_type": "结构体字段类型",
74-
"gorm_options": "gorm配置项",
75-
"binding_options": "binding配置项(不包含required,required由struct_field_required控制)"
76-
}
77-
]
78-
}
63+
```yaml
64+
---
65+
name: 结构体名称
66+
comment: 结构体注释说明
67+
fields:
68+
- name: 结构体字段名称
69+
type: 结构体字段类型
70+
comment: 结构体字段注释
71+
required: 结构体字段是否是必选项
72+
binding_options: binding配置项(不包含required,required由required字段控制)
73+
gorm_options: gorm配置项
7974
```
8075
8176
> 使用示例
8277
83-
> 创建`task.json`文件
84-
85-
```json
86-
{
87-
"struct_name": "Task",
88-
"comment": "任务管理",
89-
"fields": [
90-
{
91-
"struct_field_name": "RecordID",
92-
"comment": "记录ID",
93-
"struct_field_required": false,
94-
"struct_field_type": "string",
95-
"gorm_options": "size:36;index;",
96-
"binding_options": ""
97-
},
98-
{
99-
"struct_field_name": "Name",
100-
"comment": "任务名称",
101-
"struct_field_required": true,
102-
"struct_field_type": "string",
103-
"gorm_options": "size:200;index;",
104-
"binding_options": ""
105-
},
106-
{
107-
"struct_field_name": "Memo",
108-
"comment": "备注",
109-
"struct_field_required": false,
110-
"struct_field_type": "string",
111-
"gorm_options": "size:1024;",
112-
"binding_options": ""
113-
},
114-
{
115-
"struct_field_name": "Status",
116-
"comment": "状态(1:启用 2:停用)",
117-
"struct_field_required": true,
118-
"struct_field_type": "int",
119-
"gorm_options": "",
120-
"binding_options": "max=2,min=1"
121-
},
122-
{
123-
"struct_field_name": "Creator",
124-
"comment": "创建者",
125-
"struct_field_required": false,
126-
"struct_field_type": "string",
127-
"gorm_options": "size:36;index;",
128-
"binding_options": ""
129-
}
130-
]
131-
}
78+
> 创建`task.yaml`文件
79+
80+
``` yaml
81+
name: Task
82+
comment: 任务管理
83+
fields:
84+
- name: Code
85+
type: string
86+
comment: 任务编号
87+
required: true
88+
binding_options: ""
89+
gorm_options: "size:50;index;"
90+
- name: Name
91+
type: string
92+
comment: 任务名称
93+
required: true
94+
binding_options: ""
95+
gorm_options: "size:50;index;"
96+
- name: Memo
97+
type: string
98+
comment: 任务备注
99+
required: false
100+
binding_options: ""
101+
gorm_options: "size:1024;"
132102
```
133103

134104
```bash
135-
$ gin-admin-cli g -d ./test-gin-admin -p test-gin-admin -f task.json
105+
$ gin-admin-cli g -p test-gin-admin -f task.yaml
136106
```
137107

138108
## MIT License
139109

140-
Copyright (c) 2019 Lyric
110+
Copyright (c) 2020 Lyric

‎cmd/cmd.go

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package cmd
22

33
import (
4+
"errors"
45
"fmt"
6+
"os"
7+
"path/filepath"
58

69
"github.com/LyricTian/gin-admin-cli/cmd/generate"
710
"github.com/LyricTian/gin-admin-cli/cmd/new"
@@ -16,15 +19,15 @@ func NewCommand() cli.Command {
1619
Flags: []cli.Flag{
1720
&cli.StringFlag{
1821
Name: "dir, d",
19-
Usage: "项目生成目录",
22+
Usage: "项目生成目录(默认GOPATH)",
2023
},
2124
&cli.StringFlag{
2225
Name: "pkg, p",
2326
Usage: "项目包名",
2427
},
25-
&cli.BoolFlag{
26-
Name: "core, c",
27-
Usage: "使用core分支",
28+
&cli.StringFlag{
29+
Name: "branch, b",
30+
Usage: "指定分支(默认master)",
2831
},
2932
&cli.BoolFlag{
3033
Name: "mirror, m",
@@ -40,9 +43,24 @@ func NewCommand() cli.Command {
4043
Dir: c.String("dir"),
4144
PkgName: c.String("pkg"),
4245
UseMirror: c.Bool("mirror"),
43-
UseCore: c.Bool("core"),
46+
Branch: c.String("branch"),
4447
IncludeWeb: c.Bool("web"),
4548
}
49+
50+
if cfg.PkgName == "" {
51+
return errors.New("请指定包名")
52+
}
53+
54+
if cfg.Dir == "" {
55+
vpath := os.Getenv("GOPATH")
56+
if vpath == "" {
57+
return errors.New("请指定dir或者设置GOPATH")
58+
}
59+
cfg.Dir = filepath.Join(vpath, "src", cfg.PkgName)
60+
}
61+
62+
cfg.AppName = filepath.Base(cfg.PkgName)
63+
4664
return new.Exec(cfg)
4765
},
4866
}
@@ -55,25 +73,14 @@ func GenerateCommand() cli.Command {
5573
Aliases: []string{"g"},
5674
Flags: []cli.Flag{
5775
&cli.StringFlag{
58-
Name: "dir, d",
59-
Usage: "项目生成目录",
60-
Required: true,
76+
Name: "dir, d",
77+
Usage: "项目生成目录(默认GOPATH)",
6178
},
6279
&cli.StringFlag{
6380
Name: "pkg, p",
6481
Usage: "项目包名",
6582
Required: true,
6683
},
67-
&cli.StringFlag{
68-
Name: "ctl",
69-
Usage: "控制器swagger模板(支持default(基于github.com/swaggo/swag)和tb(基于github.com/teambition/swaggo))",
70-
Value: "default",
71-
},
72-
&cli.StringFlag{
73-
Name: "router",
74-
Usage: "路由模块(routers/api/api.go)",
75-
Value: "api",
76-
},
7784
&cli.StringFlag{
7885
Name: "name, n",
7986
Usage: "业务模块名称(结构体名称)",
@@ -84,33 +91,41 @@ func GenerateCommand() cli.Command {
8491
},
8592
&cli.StringFlag{
8693
Name: "file, f",
87-
Usage: "指定模板文件(.json,模板配置可参考说明)",
94+
Usage: "指定模板文件(.yaml,模板配置可参考说明)",
8895
},
8996
&cli.StringFlag{
9097
Name: "module, m",
91-
Usage: "指定生成模块(以逗号分隔,支持:all,schema,entity,model,bll,router)",
98+
Usage: "指定生成模块(默认生成全部模块,以逗号分隔,支持:schema,model,bll,api,mock,router)",
99+
},
100+
&cli.StringFlag{
101+
Name: "storage, s",
102+
Usage: "指定model的实现存储(默认gorm,支持:mongo/gorm)",
92103
},
93104
},
94105
Action: func(c *cli.Context) error {
95106
cfg := generate.Config{
96-
Dir: c.String("dir"),
97-
PkgName: c.String("pkg"),
98-
CtlTpl: c.String("ctl"),
99-
RouterName: c.String("router"),
100-
Name: c.String("name"),
101-
Comment: c.String("comment"),
102-
File: c.String("file"),
103-
Modules: c.String("module"),
107+
Dir: c.String("dir"),
108+
PkgName: c.String("pkg"),
109+
Name: c.String("name"),
110+
Comment: c.String("comment"),
111+
File: c.String("file"),
112+
Modules: c.String("module"),
113+
Storage: c.String("storage"),
104114
}
105115

106116
if cfg.Dir == "" {
107-
fmt.Println("请指定项目目录")
108-
return nil
109-
} else if cfg.PkgName == "" {
117+
vpath := os.Getenv("GOPATH")
118+
if vpath == "" {
119+
return errors.New("请指定dir或者设置GOPATH")
120+
}
121+
cfg.Dir = filepath.Join(vpath, "src", cfg.PkgName)
122+
}
123+
124+
if cfg.PkgName == "" {
110125
fmt.Println("请指定包名")
111126
return nil
112127
} else if cfg.Name == "" && cfg.File == "" {
113-
fmt.Println("请指定模块名称或模板文件")
128+
fmt.Println("请指定模块名称或模板配置文件")
114129
return nil
115130
} else if cfg.Name != "" && cfg.Comment == "" {
116131
fmt.Println("请指定模块说明")

0 commit comments

Comments
 (0)