-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
195 lines (159 loc) · 4.22 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"errors"
"flag"
"fmt"
"html/template"
"log"
"os"
"strings"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
func main() {
err := run(os.Args)
if err != nil {
log.Println(err)
os.Exit(1)
}
}
func run(args []string) error {
var exclude = StringArrayFromFile{}
var include = StringArrayFromFile{}
var flag = flag.NewFlagSet(args[0], flag.ExitOnError)
sqlite := flag.Bool("sqlite", false, "Access a SQLite database.")
postgres := flag.Bool("postgres", false, "Access a PostgreSQL database defined by PGDATABASE_URL environment variable")
listTables := flag.Bool("list", false, "List table names to standard output.")
includeFK := flag.Bool("fk", false, "Add FK information to table output.")
truncateDot := flag.Int("truncate", -1, "In Graphviz output, truncate large tables to `n` columns.")
mdOutput := flag.Bool("md", false, "Output using Markdown.")
htmlOutput := flag.Bool("html", false, "Output using HTML.")
d2Output := flag.Bool("d2", false, "Output using D2.")
templateFile := flag.String("template", "", "Alternative Go template file.")
section := flag.Int("section", 1, "Generate <h`N` /> elements (or equivalent Markdown)")
flag.Var(&exclude, "exclude-file", "Tables or views to exclude, read from a file.")
flag.Var(&include, "include-file", "Tables or views to include, read from a file.")
flag.Var(&exclude.List, "exclude", "Tables or views to exclude.")
flag.Var(&include.List, "include", "Tables or views to include.")
flag.Parse(args[1:])
if len(exclude.List) > 0 && len(include.List) > 0 {
return errors.New("error: provide only one of --include or --exclude")
}
// validate user provided template now.
var userTemplate *template.Template
if *templateFile != "" {
var err error
if userTemplate, err = newTemplateFromFile(*templateFile); err != nil {
return err
}
}
// validate section: 1 <= section <= 6
if *section < 1 || *section > 6 {
*section = 1
}
// Read tables from the specified source
var tables Tables
switch {
case *sqlite:
db, err := sqlx.Open("sqlite3", flag.Arg(0))
if err != nil {
return err
}
defer db.Close()
tables, err = parseSqlite(db)
if err != nil {
return err
}
case *postgres:
db, err := sqlx.Open("pgx", os.Getenv("PGDATABASE_URL"))
if err != nil {
return err
}
defer db.Close()
tables, err = parsePostgres(db)
if err != nil {
return err
}
default:
return errors.New("error: provide -sqlite or -postgres")
}
// Filter tables
tables = filter(tables, exclude.List, include.List)
// Output requested results
switch {
case *listTables:
for _, t := range tables {
fmt.Println(t.Name)
}
return nil
case userTemplate != nil:
if err := userTemplate.Execute(os.Stdout, tables); err != nil {
return err
}
return nil
case *mdOutput:
return md(os.Stdout, tables, *section)
case *htmlOutput:
return html(os.Stdout, tables, *section)
case *d2Output:
return d2(os.Stdout, tables)
default:
return graph(os.Stdout, tables, *includeFK, *truncateDot)
}
}
func filter(tables []Table, exclude, include []string) []Table {
if len(exclude) == 0 && len(include) == 0 {
return tables
}
var newTables []Table
fn := newFilter(exclude, include)
// remove tables
for _, t := range tables {
if fn(t.Name) {
newTables = append(newTables, t)
}
}
return newTables
}
func newFilter(exclude, include []string) func(n string) bool {
var m = make(map[string]struct{})
var wildcard = make([]string, 0)
var fnNeg = func(n string) bool {
n = strings.ToLower(n)
if _, ok := m[n]; ok {
return false
}
return !searchWildcard(n, wildcard)
}
var fnPos = func(n string) bool {
n = strings.ToLower(n)
if _, ok := m[n]; ok {
return true
}
return searchWildcard(n, wildcard)
}
var fn = fnPos
var src = include
if len(exclude) > 0 {
src = exclude
fn = fnNeg
}
for _, k := range src {
k = strings.ToLower(k)
if i := strings.LastIndexByte(k, '*'); i >= 0 {
wildcard = append(wildcard, k[:i])
continue
}
m[strings.ToLower(k)] = struct{}{}
}
return fn
}
func searchWildcard(n string, wildcards []string) bool {
for i := range wildcards {
if strings.HasPrefix(n, wildcards[i]) {
return true
}
}
return false
}