-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
153 lines (126 loc) · 2.73 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
package main
//go:generate ./gen-license.sh
import (
"fmt"
"io"
"os"
"os/user"
"strings"
"github.com/alexflint/go-arg"
"github.com/knq/usql/drivers"
"github.com/knq/usql/env"
"github.com/knq/usql/handler"
"github.com/knq/usql/internal"
"github.com/knq/usql/rline"
"github.com/knq/usql/text"
)
func main() {
// get available drivers and known build tags
available, known := drivers.Available(), internal.KnownBuildTags()
// report if database is supported
if len(os.Args) == 2 &&
strings.HasPrefix(os.Args[1], "--has-") &&
strings.HasSuffix(os.Args[1], "-support") {
n := os.Args[1][6 : len(os.Args[1])-8]
if v, ok := known[n]; ok {
n = v
}
var out int
if _, ok := available[n]; ok {
out = 1
}
fmt.Fprintf(os.Stdout, "%d", out)
return
}
var err error
// load current user
cur, err := user.Current()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
// parse args
args := &Args{
Username: cur.Username,
}
arg.MustParse(args)
// run
err = run(args, cur)
if err != nil && err != io.EOF && err != rline.ErrInterrupt {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
if e, ok := err.(*drivers.Error); ok {
// extra output for when a driver is not available
if e.Err == text.ErrDriverNotAvailable {
tag := e.Driver
if t, ok := known[tag]; ok {
tag = t
}
fmt.Fprint(os.Stderr, "\ntry:\n\n go get -u -tags "+tag+" github.com/knq/usql\n\n")
}
}
os.Exit(1)
}
}
// run processes args, processing args.Commands if non-empty, or args.File if
// specified, otherwise launch an interactive readline from stdin.
func run(args *Args, u *user.User) error {
var err error
// get working directory
wd, err := os.Getwd()
if err != nil {
return err
}
// create input/output
l, err := rline.New(args.Commands, args.File, args.Out, env.HistoryFile(u))
if err != nil {
return err
}
defer l.Close()
// create handler
h := handler.New(l, u, wd, args.NoPassword)
// force a password ...
dsn := args.DSN
if args.ForcePassword {
dsn, err = h.Password(dsn)
if err != nil {
return err
}
}
// open dsn
err = h.Open(dsn)
if err != nil {
return err
}
// start transaction
if args.SingleTransaction {
if h.IO().Interactive() {
return text.ErrSingleTransactionCannotBeUsedWithInteractiveMode
}
err = h.Begin()
if err != nil {
return err
}
}
// rc file
if rc := env.RCFile(u); !args.NoRC && rc != "" {
err = h.Include(rc, false)
if err != nil && err != text.ErrNoSuchFileOrDirectory {
return err
}
}
// setup runner
f := h.Run
if len(args.Commands) != 0 {
f = h.CommandRunner(args.Commands)
}
// run
err = f()
if err != nil {
return err
}
// commit
if args.SingleTransaction {
return h.Commit()
}
return nil
}