-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (80 loc) · 1.69 KB
/
main.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
package main
import (
"log"
"os"
"os/exec"
"github.com/donnol/jdnote/models/action"
"github.com/donnol/jdnote/models/role"
"github.com/donnol/jdnote/models/roleaction"
"github.com/donnol/jdnote/models/user"
"github.com/donnol/jdnote/models/userrole"
"github.com/donnol/tools/dbdoc"
)
func main() {
makeDoc()
makeGraph()
}
func makeDoc() {
// 打开文件
filename := "DB_README.md"
w, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_RDWR, os.ModePerm)
if err != nil {
panic(err)
}
defer w.Close()
// 标题
_, err = w.WriteString("# 数据库表定义\n\n![ER图](./db_cicada.svg)\n\n")
if err != nil {
panic(err)
}
// 解析
if err := dbdoc.Resolve(w,
&user.Entity{},
&role.Entity{},
&userrole.Entity{},
&action.Entity{},
&roleaction.Entity{},
); err != nil {
panic(err)
}
}
func makeGraph() {
filename := "DB_cicada.dot"
makeDot(filename)
execDot(filename)
}
func makeDot(filename string) {
// 打开文件
w, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_RDWR, os.ModePerm)
if err != nil {
panic(err)
}
defer w.Close()
// 解析
if err := dbdoc.ResolveGraph(w,
&user.Entity{},
&role.Entity{},
&userrole.Entity{},
&action.Entity{},
&roleaction.Entity{},
); err != nil {
panic(err)
}
}
func execDot(filename string) {
// 如果有安装dot命令,则执行命名生成svg图
cmdName := "dot"
if _, err := exec.LookPath(cmdName); err == nil {
// dot -Tpng -o hello.png tmp.dot
cmd := exec.Command(cmdName, "-Tsvg", "-odb_cicada.svg", filename)
out, err := cmd.Output()
if err != nil {
panic(err)
}
if len(out) != 0 {
log.Printf("Output: %s\n", out)
}
} else {
log.Printf("Not find dot command\n")
}
}