-
Notifications
You must be signed in to change notification settings - Fork 34
/
commands.go
184 lines (168 loc) · 4.22 KB
/
commands.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type commands struct {
l *Logic
}
func NewCommands(logic *Logic) *commands {
return &commands{
l: logic,
}
}
//映射相应的命令
func (c *commands) Handlers() map[string]func(args []string) int {
return map[string]func(args []string) int{
"0": c.CustomDir,
"1": c.MarkDown,
"2": c.GenerateEntry,
"3": c.GenerateCURD,
"4": c.CustomFormat,
"5": c.ShowTableList,
"7": c.Clean,
"clear": c.Clean,
"c": c.Clean,
"8": c.Help,
"h": c.Help,
"help": c.Help,
"ll": c.Help,
"ls": c.Help,
"quit": c.Quit,
"q": c.Quit,
"exit": c.Quit,
}
}
//生成数据库表的markdown文档
func (c *commands) MarkDown(args []string) int {
fmt.Println("Preparing to generate the markdown document...")
//检查目录是否存在
CreateDir(c.l.Path)
err := c.l.CreateMarkdown()
if err != nil {
log.Println("MarkDown>>", err)
}
return 0
}
//help list
func (c *commands) Help(args []string) int {
for _, row := range CmdHelp {
s := fmt.Sprintf("%s %s\n", "NO:"+row.No, row.Msg)
fmt.Print(s)
}
return 0
}
//生成golang表对应的结构实体
func (c *commands) GenerateEntry(args []string) int {
fmt.Print("Do you need to set the format of the structure?(Yes|No)>")
line, _, _ := bufio.NewReader(os.Stdin).ReadLine()
switch strings.ToLower(string(line)) {
case "yes", "y":
formats = c._setFormat()
}
err := c.l.CreateEntity(formats)
if err != nil {
log.Println("GenerateEntry>>", err.Error())
}
go Gofmt(GetExeRootDir())
return 0
}
//还可以自定义结构体解析实体,如json,gorm,xml
func (c *commands) CustomFormat(args []string) int {
formats = c._setFormat()
return 0
}
//生成golang操作mysql的CRUD增删改查语句
func (c *commands) GenerateCURD(args []string) int {
err := c.l.CreateCURD(formats)
if err != nil {
log.Println("GenerateCURD>>", err.Error())
}
go Gofmt(GetExeRootDir())
return 0
}
//自定义生成目录
func (c *commands) CustomDir(args []string) int {
fmt.Print("Please set the build directory>")
line, _, _ := bufio.NewReader(os.Stdin).ReadLine()
if string(line) != "" {
path, err := c.l.T.GenerateDir(string(line))
if err == nil {
c.l.Path = path
fmt.Println("Directory success:", path)
} else {
log.Println("Set directory failed>>", err)
}
}
return 0
}
//显示所有的表名
func (c *commands) ShowTableList(args []string) int {
if len(c.l.DB.Tables) == 0 {
fmt.Println("Whoops, Nothing at all!!!")
return 0
}
c._showTableList(c.l.DB.Tables)
fmt.Print("Select the table sequence number you need?(By default all, comma separated,all represents all)>")
line, _, _ := bufio.NewReader(os.Stdin).ReadLine()
if !strings.EqualFold(string(line), "") {
c.l.DB.DoTables = c._filterTables(string(line), c.l.DB.Tables)
}
return 0
}
//清屏
func (c *commands) Clean(args []string) int {
Clean()
return 0
}
//退出
func (c *commands) Quit(args []string) int {
return 1
}
//过滤表名
func (c *commands) _filterTables(ids string, tables []TableNameAndComment) []TableNameAndComment {
lst := strings.Split(ids, ",")
result := make([]TableNameAndComment, 0)
if strings.ToLower(ids) == "all" {
return tables
}
for _, id := range lst {
id = strings.TrimSpace(id)
for _, t := range tables {
if strconv.Itoa(t.Index) == id || id == t.Name {
result = append(result, t)
}
}
}
return result
}
//显示所有名视图
func (c *commands) _showTableList(NameAndComment []TableNameAndComment) {
for idx, table := range NameAndComment {
idx++
info := fmt.Sprintf("%s:%s", strconv.Itoa(idx), table.Name)
if table.Comment != "" {
info += fmt.Sprintf("(%s)", table.Comment)
}
fmt.Println(info)
}
fmt.Println("Total " + strconv.Itoa(len(NameAndComment)) + " tables\n")
}
//set struct format
func (c *commands) _setFormat() []string {
fmt.Print("Set the mapping name of the structure, separated by a comma (example :json,gorm)>")
input, _, _ := bufio.NewReader(os.Stdin).ReadLine()
if string(input) != "" {
formatList := CheckCharDoSpecialArr(string(input), ',', `[\w\,\-]+`)
if len(formatList) > 0 {
fmt.Printf("Set format success: %v\n", formatList)
return formatList
}
}
fmt.Println("Set failed")
return nil
}