-
Notifications
You must be signed in to change notification settings - Fork 34
/
core.go
158 lines (146 loc) · 3.69 KB
/
core.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
package main
import (
"bufio"
"database/sql"
"errors"
"fmt"
"log"
"os"
"strings"
"github.com/howeyc/gopass"
"github.com/urfave/cli/v2"
)
var (
Conn *sql.DB //连接对象
DbConn DBConfig //db config
formats []string //format
)
//命令行实现
func start() {
usage()
go release()
DbConn.MaxIdleConn = 5
DbConn.MaxOpenConn = 10
app.Action = func(c *cli.Context) error {
if c.Bool("debug") {
log.SetFlags(log.Lshortfile | log.LstdFlags)
}
DbConn.Host = c.String("h") // 数据库地址
DbConn.Name = c.String("u") // 数据库用户名称
DbConn.Port = c.Int("P") // 端口号
DbConn.Pass = c.String("p") // 密码
DbConn.Charset = c.String("c") // 编码格式
if c.NumFlags() > 0 {
dbName := c.String("d") // 数据库名称
if dbName == "" {
return cli.NewExitError("database is null, please use -d params", 9)
}
DbConn.DBName = dbName
if DbConn.Pass == "" { // Use an asterisk instead of the password you entered
pass, err := gopass.GetPasswdPrompt("Enter password:", false, os.Stdin, os.Stdout)
if err != nil {
return err
}
DbConn.Pass = string(pass)
Clean()
}
if err := Commands(); err != nil {
return cli.NewExitError(err, 1)
}
}
return nil
}
// cli start run
if err := app.Run(os.Args); err != nil {
log.Fatal("RUN>>", err)
}
}
//释放资源
func release() {
<-stop
_ = Conn.Close()
}
//构建命令使用说明
func usage() {
app.Name = ProjectName //项目名称
app.Authors = []*cli.Author{{
Name: Author,
Email: AuthorEmail,
}}
app.Version = Version //版本号
app.Copyright = "@Copyright " + Copyright //版权保护
app.Usage = "Quickly generate CURD and documentation for operating MYSQL.etc" //说明
app.Commands = []*cli.Command{
{
Name: "help",
Aliases: []string{"h", "?"},
Usage: "show help",
Action: func(c *cli.Context) error {
_ = cli.ShowAppHelp(c)
return nil
},
},
{
Name: "version",
Aliases: []string{"v"},
Usage: "print the version",
Action: func(c *cli.Context) error {
cli.ShowVersion(c)
return nil
},
},
}
app.HideVersion = true
app.HideHelp = true
app.Flags = []cli.Flag{
&cli.StringFlag{Name: "h", Value: "127.0.0.1", Usage: "Database address"},
&cli.IntFlag{Name: "P", Value: 3306, Usage: "port number"},
&cli.StringFlag{Name: "u", Value: "root", Usage: "database username", Required: true},
&cli.StringFlag{Name: "p", Value: "", Usage: "database password", Required: true, DefaultText: ""},
&cli.StringFlag{Name: "c", Value: "utf8mb4", Usage: "database format"},
&cli.StringFlag{Name: "d", Usage: "database name"},
&cli.BoolFlag{Name: "debug", Usage: "debug", Value: false},
}
}
// 实现命令功能
func Commands() error {
var err error
Conn, err = InitDB(DbConn)
if Conn == nil || err != nil {
return errors.New("database connect failed>>" + err.Error())
}
//初使工作
DbModel := NewDB()
DbModel.Using(Conn)
DbModel.DBName = DbConn.DBName
logic := &Logic{
DB: DbModel,
Path: GetExeRootDir() + DefaultSavePath + DS, //默认当前命令所在目录
}
err = logic.DB.GetTableNameAndComment()
if err != nil {
return err
}
commands := NewCommands(logic)
commands.Help(nil)
handlers := commands.Handlers()
br := bufio.NewReader(os.Stdin)
for {
fmt.Print("-> # ")
line, _, _ := br.ReadLine()
if len(line) == 0 {
continue
}
tokens := strings.Split(string(line), " ")
if handler, ok := handlers[strings.ToLower(tokens[0])]; ok {
ret := handler(tokens)
if ret != 0 {
break
}
} else {
fmt.Println("Unknown command>>", tokens[0])
}
}
stop <- true
return nil
}