-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
76 lines (62 loc) · 1.26 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
package main
import (
"database/sql"
"errors"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jessevdk/go-flags"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/relops/sqlc/sqlc"
"log"
"os"
)
var VERSION string = "0.1.5"
var opts sqlc.Options
var parser = flags.NewParser(&opts, flags.Default)
func init() {
opts.Version = printVersionAndExit
}
func main() {
if _, err := parser.Parse(); err != nil {
os.Exit(1)
}
if err := opts.Validate(); err != nil {
log.Fatal(err)
os.Exit(1)
}
db, dialect, err := dataSource()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
opts.Dialect = dialect
err = sqlc.Generate(db, VERSION, &opts)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
}
func dataSource() (*sql.DB, sqlc.Dialect, error) {
d, err := opts.DbType()
if err != nil {
return nil, sqlc.Sqlite, err
}
switch d {
case sqlc.Sqlite:
db, err := sql.Open("sqlite3", opts.File)
return db, d, err
case sqlc.MySQL:
db, err := sql.Open("mysql", opts.Url)
return db, d, err
case sqlc.Postgres:
db, err := sql.Open("postgres", opts.Url)
return db, d, err
default:
return nil, sqlc.Sqlite, errors.New("Invalid Db type")
}
}
func printVersionAndExit() {
fmt.Fprintf(os.Stderr, "%s %s\n", "sqlc", VERSION)
os.Exit(0)
}