This repository has been archived by the owner on Jul 26, 2019. It is now read-only.
/
engine.go
114 lines (104 loc) · 2.47 KB
/
engine.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
package parser
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"os"
"os/exec"
"path"
)
//ParseEngine parser engine
type ParseEngine struct {
db *sql.DB
database string
OutPath string
OutPackage string
MysqlAddr string
MysqlUser string
MysqlPassword string
MysqlDatabase string
MysqlCharset string
AppSecret string
AppListen string
Tables []*schema
AuthTable string
AuthPassword string
}
//NewParseEngine create a parser engine
func NewGuiParseEngine(mysqlUser, mysqlPassword, mysqlAddr, mysqlDatabase, mysqlCharset, outDir, appListen, authTable, authColumn string) (*ParseEngine, error) {
conn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=%s", mysqlUser, mysqlPassword, mysqlAddr, mysqlDatabase, mysqlCharset)
db, err := sql.Open("mysql", conn)
if err != nil {
return nil, err
}
projectPath := path.Join(goPath, "src", outDir)
return &ParseEngine{
db,
mysqlDatabase,
projectPath,
outDir,
mysqlAddr,
mysqlUser,
mysqlPassword,
mysqlDatabase,
mysqlCharset,
randomString(32),
appListen,
[]*schema{},
authTable,
authColumn,
}, nil
}
func NewParseEngine(mysqlUser, mysqlPassword, mysqlAddr, mysqlDatabase, mysqlCharset, outDir, appListen, authTable, authColumn string) *ParseEngine {
conn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=%s", mysqlUser, mysqlPassword, mysqlAddr, mysqlDatabase, mysqlCharset)
db, err := sql.Open("mysql", conn)
if err != nil {
log.Println(err)
}
projectPath := path.Join(goPath, "src", outDir)
return &ParseEngine{
db,
mysqlDatabase,
projectPath,
outDir,
mysqlAddr,
mysqlUser,
mysqlPassword,
mysqlDatabase,
mysqlCharset,
randomString(32),
appListen,
[]*schema{},
authTable,
authColumn,
}
}
//Close the mysql database
func (p *ParseEngine) Close() {
p.db.Close()
}
func (p *ParseEngine) makeModuleDir(module string) {
modulePath := path.Join(p.OutPath, module)
if err := os.MkdirAll(modulePath, 0777); err != nil {
log.Fatal(err)
}
}
//GoFmt run gofmt for the generated project
func (p *ParseEngine) GoFmt() {
log.Println("running go fmt for the new project")
runCmd("go", "fmt", p.OutPackage+"/...")
//mainPath := path.Join(p.OutPath,"main.go")
//runCmd("go","run",mainPath)
log.Printf("cd %s", p.OutPath)
log.Println("test your project")
}
func runCmd(name string, args ...string) {
cmd := exec.Command(name, args...)
out, err := cmd.Output()
if err != nil {
log.Println(err)
} else {
log.Printf("%s", out)
}
}