-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (69 loc) · 1.81 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
package main
import (
"io"
"log"
"os"
"github.com/alecthomas/kingpin"
)
var (
connStr = kingpin.Arg(
"conn", "MySQL/PostgreSQL connection string in URL format").Required().String()
driver = kingpin.Flag("driver", "driver mysql/postgres, Default mysql").Default("mysql").Short('d').String()
postgresSchema = kingpin.Flag(
"schema", "PostgreSQL schema name").Default("public").Short('s').String()
outFile = kingpin.Flag("output", "output file path").Short('o').String()
targetTbls = kingpin.Flag("table", "target tables").Short('t').Strings()
xTargetTbls = kingpin.Flag("exclude", "target tables").Short('x').Strings()
title = kingpin.Flag("title", "Diagram title").Short('T').String()
svg = kingpin.Flag("svg", "gen svg").Bool()
)
func main() {
kingpin.Parse()
var planter Planter
switch *driver {
case "mysql":
planter = NewMysql()
case "postgres":
planter = NewPostgres(*postgresSchema)
default:
log.Fatal("unknown driver")
}
planter.OpenDB(*connStr)
ts := planter.LoadTableDef()
// use foreign key analysis if all table not set fk
ForeignKeyAnalysis(ts)
var tbls []*Table
if len(*targetTbls) != 0 {
tbls = FilterTables(true, ts, *targetTbls)
} else {
tbls = ts
}
if len(*xTargetTbls) != 0 {
tbls = FilterTables(false, tbls, *xTargetTbls)
}
entry, err := TableToUMLEntry(tbls)
if err != nil {
log.Fatal(err)
}
rel, err := ForeignKeyToUMLRelation(tbls)
if err != nil {
log.Fatal(err)
}
src := writePrefix(entry, rel, *title)
// save as svg
if svg != nil && *svg {
src = genSVG(string(src))
}
var out io.Writer
if *outFile != "" {
out, err = os.Create(*outFile)
if err != nil {
log.Fatalf("failed to create output file %s: %s", *outFile, err)
}
} else {
out = os.Stdout
}
if _, err := out.Write(src); err != nil {
log.Fatal(err)
}
}