-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
175 lines (169 loc) · 3.83 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
// Command usql is the universal command-line interface for SQL databases.
package main
//go:generate go run gen.go
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/user"
"strings"
"github.com/xo/usql/drivers"
"github.com/xo/usql/env"
"github.com/xo/usql/handler"
"github.com/xo/usql/internal"
"github.com/xo/usql/rline"
"github.com/xo/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
}
// load current user
cur, err := user.Current()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
args := NewArgs()
// run
err = run(args, cur)
if err != nil && err != io.EOF && err != rline.ErrInterrupt {
var he *handler.Error
if !errors.As(err, &he) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
}
var e *drivers.Error
if errors.As(err, &e) && e.Err == text.ErrDriverNotAvailable {
m := make(map[string]string, len(known))
for k, v := range known {
m[v] = k
}
tag := e.Driver
if t, ok := m[tag]; ok {
tag = t
}
fmt.Fprintf(os.Stderr, "\ntry:\n\n go get -u -tags %s github.com/xo/usql\n\n", tag)
}
os.Exit(1)
}
}
// run processes args, processing args.CommandOrFiles if non-empty, if
// specified, otherwise launch an interactive readline from stdin.
func run(args *Args, u *user.User) error {
// get working directory
wd, err := os.Getwd()
if err != nil {
return err
}
// handle variables
for _, v := range args.Variables {
if i := strings.Index(v, "="); i != -1 {
_ = env.Set(v[:i], v[i+1:])
} else {
_ = env.Unset(v)
}
}
for _, v := range args.PVariables {
if i := strings.Index(v, "="); i != -1 {
vv := v[i+1:]
if c := vv[0]; c == '\'' || c == '"' {
var err error
vv, err = env.Dequote(vv, c)
if err != nil {
return err
}
}
if _, err = env.Pset(v[:i], vv); err != nil {
return err
}
} else {
if _, err = env.Ptoggle(v, ""); err != nil {
return err
}
}
}
// create input/output
l, err := rline.New(len(args.CommandOrFiles) != 0, 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
if err = h.Open(context.Background(), dsn); err != nil {
return err
}
// start transaction
if args.SingleTransaction {
if h.IO().Interactive() {
return text.ErrSingleTransactionCannotBeUsedWithInteractiveMode
}
if err = h.BeginTx(context.Background(), nil); err != nil {
return err
}
}
// rc file
if rc := env.RCFile(u); !args.NoRC && rc != "" {
if err = h.Include(rc, false); err != nil && err != text.ErrNoSuchFileOrDirectory {
return err
}
}
// setup runner
f := h.Run
if len(args.CommandOrFiles) != 0 {
f = runCommandOrFiles(h, args.CommandOrFiles)
}
// run
if err = f(); err != nil {
return err
}
// commit
if args.SingleTransaction {
return h.Commit()
}
return nil
}
// runCommandOrFiles proccesses all the supplied commands or files.
func runCommandOrFiles(h *handler.Handler, commandsOrFiles []CommandOrFile) func() error {
return func() error {
for _, x := range commandsOrFiles {
h.SetSingleLineMode(x.Command)
if x.Command {
h.Reset([]rune(x.Value))
if err := h.Run(); err != nil {
return err
}
} else {
if err := h.Include(x.Value, false); err != nil {
return err
}
}
}
return nil
}
}