-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.go
69 lines (60 loc) · 1.66 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
package main
import (
"io"
"log"
"os"
"os/exec"
"gopkg.in/alecthomas/kingpin.v2"
)
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()
pkgName = kingpin.Flag("package", "package name").Default("main").Short('p').String()
typeMapFilePath = kingpin.Flag("typemap", "column type and go type map file path").Short('t').String()
exTbls = kingpin.Flag("exclude", "table names to exclude").Short('x').Strings()
customTmpl = kingpin.Flag("template", "custom template path").String()
outFile = kingpin.Flag("output", "output file path").Short('o').String()
noQueryInterface = kingpin.Flag("no-interface", "output without Queryer interface").Bool()
version string
)
func init() {
kingpin.Version(version)
}
func main() {
kingpin.Parse()
conn, err := OpenDB(*connStr)
if err != nil {
log.Fatal(err)
}
st, err := PgCreateStruct(conn, *schema, *typeMapFilePath, *pkgName, *customTmpl, *exTbls)
if err != nil {
log.Fatal(err)
}
var src []byte
if *noQueryInterface {
src = st
} else {
q := []byte(queryInterface)
src = append(st, q...)
}
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)
}
if *outFile != "" {
params := []string{"-w", *outFile}
if err := exec.Command("goimports", params...).Run(); err != nil {
log.Fatalf("failed to goimports: %s", err)
}
}
}