-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
78 lines (65 loc) · 1.98 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
package main
import (
"fmt"
"log"
"os"
"os/user"
flags "github.com/jessevdk/go-flags"
_ "github.com/lib/pq"
)
type Options struct {
Host string `short:"s" long:"server" description:"Server name or IP address" default:"127.0.0.1"`
Port int `short:"p" long:"port" description:"Port" default:"5432"`
User string `short:"u" long:"user" description:"Database user."`
Password string `short:"w" long:"password" description:"Database password."`
Database string `short:"d" long:"database" description:"Database name." required:"true"`
Tables []string `short:"t" long:"tables" description:"Tables to export." required:"true"`
SSLMode string `long:"ssl" description:"SSL mode [require|verify-full|verify-ca|disable]" default:"disable"`
FilePerTable bool `short:"f" long:"file-per-table" description:"Save each structure to its own .go file."`
PackageName string `long:"package" description:"Package name for generated files."`
}
func main() {
var options Options
parser := flags.NewParser(&options, flags.Default)
if _, err := parser.Parse(); err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
}
os.Exit(1)
}
if options.User == "" {
//obtain current logged user
u, err := user.Current()
if err != nil {
log.Fatalln("unable to get current os user: ", err)
}
options.User = u.Username
}
db, err := dbConnect(options)
if err != nil {
log.Fatalf("unable to connect to database: %s", err)
}
defer db.Close()
p := "models"
if options.PackageName != "" {
p = options.PackageName
}
d := fmt.Sprintf("package %s \n", p)
if !options.FilePerTable {
fmt.Println(d)
}
for _, t := range options.Tables {
cols, err := columnList(db, t)
if err != nil {
log.Fatalln(err)
}
data := getStruct(t, cols)
if options.FilePerTable {
if err := saveToFile(t, []byte(d+data)); err != nil {
log.Fatalln(err)
}
continue
}
fmt.Println(data)
}
}