forked from caibirdme/gforge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgforge.go
133 lines (118 loc) · 3.1 KB
/
gforge.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
package main
import (
"bytes"
"database/sql"
"fmt"
"io"
"os"
"github.com/caibirdme/gforge/internal/dao"
"github.com/caibirdme/gforge/internal/schema"
"github.com/didi/gendry/manager"
_ "github.com/go-sql-driver/mysql"
"github.com/mkideal/cli"
)
func main() {
if err := cli.Root(rootCommand, cli.Tree(help), cli.Tree(schemaCommand), cli.Tree(daoCommand)).Run(os.Args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
}
}
var help = cli.HelpCommand("display help information")
const version = "0.0.1"
type rootArg struct {
cli.Helper
Version bool `cli:"v" usage:"version"`
}
var rootCommand = &cli.Command{
Name: "tools",
Desc: "A collection of tools to generate code for operating database supported by Gendry",
Argv: func() interface{} { return new(rootArg) },
Fn: func(ctx *cli.Context) error {
arg := ctx.Argv().(*rootArg)
if arg.Version {
ctx.String("%s\n", version)
return nil
}
return nil
},
}
type schemaArg struct {
DBName string `cli:"d" usage:"database name"`
TableName string `cli:"t" usage:"table name"`
UserName string `cli:"u" usage:"user name"`
Password string `cli:"p" usage:"password"`
Host string `cli:"h" usage:"host" dft:"localhost"`
Port int `cli:"P" usage:"port" dft:"3306"`
}
var schemaCommand = &cli.Command{
Name: "table",
Desc: "schema could generate go struct code for given table",
Argv: func() interface{} { return new(schemaArg) },
Fn: func(ctx *cli.Context) error {
argv := ctx.Argv().(*schemaArg)
_, err := getSchema(os.Stdout, argv)
return err
},
}
func getSchema(w io.Writer, argv *schemaArg) (string, error) {
db, err := getDBInstance(&dBConfig{
Host: argv.Host,
User: argv.UserName,
Password: argv.Password,
Port: argv.Port,
DBName: "information_schema",
})
if nil != err {
return "", err
}
return schema.GetSchema(w, db, argv.TableName, argv.DBName)
}
var daoCommand = &cli.Command{
Name: "dao",
Desc: "dao generates code of dao layer by given table name",
Argv: func() interface{} { return new(schemaArg) },
Fn: func(ctx *cli.Context) error {
arg := ctx.Argv().(*schemaArg)
var buff bytes.Buffer
io.Copy(&buff, addImport(arg.TableName))
structName, err := getSchema(&buff, arg)
if nil != err {
return err
}
r, err := dao.GenerateDao(arg.TableName, structName)
if nil != err {
return err
}
_, err = io.Copy(&buff, r)
if nil != err {
return err
}
io.Copy(os.Stdout, &buff)
return nil
},
}
//DBConfig holds the basic configuration of database
type dBConfig struct {
Host string `json:"host"`
User string `json:"user"`
Password string `json:"password"`
Port int `json:"port"`
DBName string `json:"database"`
}
func getDBInstance(conf *dBConfig) (*sql.DB, error) {
option := manager.New(conf.DBName, conf.User, conf.Password, conf.Host)
return option.Port(conf.Port).Open(true)
}
func addImport(packageName string) io.Reader {
return bytes.NewBuffer([]byte(fmt.Sprintf(`
package %s
import (
"database/sql"
"errors"
"github.com/didi/gendry/builder"
"github.com/didi/gendry/scanner"
)
/*
This code is generated by gendry
*/
`, packageName)))
}