forked from achiku/planter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (62 loc) · 1.46 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
package main
import (
"io"
"log"
"os"
"github.com/alecthomas/kingpin"
)
var (
connStr = kingpin.Arg(
"conn", "PostgreSQL connection string in URL format").Required().String()
schema = 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()
)
func main() {
kingpin.Parse()
db, err := OpenDB(*connStr)
if err != nil {
log.Fatal(err)
}
ts, err := LoadTableDef(db, *schema)
if err != nil {
log.Fatal(err)
}
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)
}
var src []byte
src = append([]byte("@startuml\n" +
"hide circle\n" +
"skinparam linetype ortho\n"), entry...)
src = append(src, rel...)
src = append(src, []byte("@enduml\n")...)
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)
}
}