-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
329 lines (263 loc) · 6.98 KB
/
Copy pathmain.go
File metadata and controls
329 lines (263 loc) · 6.98 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"bufio"
"bytes"
"compress/gzip"
"database/sql"
"encoding/csv"
"flag"
"fmt"
"io/ioutil"
"os"
"os/user"
"time"
_ "github.com/lib/pq"
)
var (
filename string
outputfilename string
command string
hostname string
port string
dbname string
username string
nopassword bool
columntitles bool
showversion bool
fielddelimiter []byte
rowdelimiter []byte
nullstring []byte
)
const VERSION = "0.0.1"
func init() {
flag.StringVar(&filename, "f", "", "execute command from file (defaults to stdin)")
flag.StringVar(&outputfilename, "o", "", "output file")
flag.StringVar(&command, "c", "", "run a single command (ignores other input)")
flag.StringVar(&hostname, "h", "", "database server host")
flag.StringVar(&port, "p", "", "database server port")
flag.StringVar(&dbname, "d", "", "database name to connect to")
flag.StringVar(&username, "u", "", "username")
flag.BoolVar(&nopassword, "w", false, "never prompt for password")
flag.BoolVar(&columntitles, "titles", false, "add row for column titles")
flag.BoolVar(&showversion, "version", false, "print version string")
}
func main() {
var err error
flag.Usage = usage
flag.Parse()
if showversion {
version()
return
}
fielddelimiter = []byte("\t")
rowdelimiter = []byte("\n")
nullstring = []byte("\\N")
user, err := user.Current()
if err != nil {
exitWithError(fmt.Errorf("unable to get current user: %s", err))
}
// unless specified, read the password from the ~/.PGPASS file or prompt
var password string
if !nopassword && username != "" {
password, err = passwordFromPgpass(user)
// TODO: when the SQL commands are also read from stdin perhaps display a
// warning
if err != nil {
fmt.Print("Enter password: ")
linereader := bufio.NewReader(os.Stdin)
b, err := linereader.ReadString('\n')
if err != nil {
exitWithError(fmt.Errorf("unable to read password: %s", err.Error()))
}
password = string(b)
}
}
// PG connections have useful defaults. Many of these are implemented
// in lib/pq so we only need to pass options through where specified.
conn := "sslmode=disable"
// TODO: support other sslmodes
if hostname != "" {
conn = fmt.Sprintf("%s host=%s", conn, hostname)
}
if username != "" {
conn = fmt.Sprintf("%s user=%s", conn, username)
}
if username != "" && password != "" {
conn = fmt.Sprintf("%s password=%s", conn, password)
}
if dbname != "" {
conn = fmt.Sprintf("%s dbname=%s", conn, dbname)
}
if port != "" {
conn = fmt.Sprintf("%s port=%s", conn, port)
}
db, err := sql.Open("postgres", conn)
if err != nil {
exitWithError(fmt.Errorf("unable to connect to postgres. %s", err))
}
defer db.Close()
// read query from input
var query string
if command != "" {
query = command
} else {
var b []byte
if filename != "" {
b, err = ioutil.ReadFile(filename)
} else {
b, err = ioutil.ReadAll(os.Stdin)
}
if err != nil {
exitWithError(fmt.Errorf("unable to read query: %s", err))
}
query = string(b)
}
rows, err := db.Query(query)
if err != nil {
exitWithError(fmt.Errorf("unable to run query: %s", err))
}
// Get column names
columns, err := rows.Columns()
if err != nil {
exitWithError(fmt.Errorf("unable to get column names: %s", err))
}
values := make([]interface{}, len(columns))
valuePtrs := make([]interface{}, len(columns))
outputfile := bufio.NewWriter(os.Stdout)
if outputfilename != "" {
file, err := os.Create(outputfilename)
if err != nil {
exitWithError(fmt.Errorf("unable to create output file %s: %s", outputfilename, err))
}
outputfile = bufio.NewWriter(file)
if len(outputfilename) > 3 && outputfilename[len(outputfilename)-3:len(outputfilename)] == ".gz" {
g := gzip.NewWriter(file)
outputfile = bufio.NewWriter(g)
defer g.Close()
}
}
defer outputfile.Flush()
if columntitles {
for i, c := range columns {
outputfile.WriteString(c)
if i < len(columns)-1 {
outputfile.Write(fielddelimiter)
} else {
outputfile.Write(rowdelimiter)
}
}
}
// build the data rows
for rows.Next() {
for i, _ := range columns {
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)
for i, _ := range columns {
outputfile.Write(StringFromPostgres(values[i]))
if i < len(columns)-1 {
outputfile.Write(fielddelimiter)
} else {
outputfile.Write(rowdelimiter)
}
}
}
}
// Convert a postgres value to a string, inferring the cell format from the
// database/sql type returned by the pg driver
func StringFromPostgres(v interface{}) []byte {
if v == nil {
return nullstring
}
switch v.(type) {
case ([]uint8):
return cleanBytesForDelimiters([]byte(v.([]uint8)), fielddelimiter, rowdelimiter)
case (bool):
if v.(bool) {
return []byte("y")
} else {
return []byte("n")
}
case (int64):
return []byte(fmt.Sprintf("%d", v))
case (float64):
return []byte(fmt.Sprintf("%f", v))
case (time.Time):
return []byte(fmt.Sprintf("%s", v.(time.Time).Format(time.RFC3339)))
default:
return cleanBytesForDelimiters([]byte(fmt.Sprintf("%s", v)), fielddelimiter, rowdelimiter)
}
}
// best effort to escape a field according to the various delimiters used
func cleanBytesForDelimiters(b []byte, fielddelimiter []byte, rowdelimiter []byte) []byte {
v := b
quote := []byte{'"'}
quoted := false
if bytes.HasPrefix(b, quote) && bytes.HasSuffix(b, quote) {
quoted = true
}
// clean according to row delimiter
if bytes.Compare(rowdelimiter, []byte("\n")) == 0 {
// double-escape
v = bytes.Replace(v, []byte("\n"), []byte("\\n"), -1)
} else if bytes.Contains(v, rowdelimiter) && !quoted {
// quote
var buf bytes.Buffer
buf.Write(quote)
buf.Write(v)
buf.Write(quote)
v = buf.Bytes()
}
// clean according to field delimiter
if bytes.Compare(fielddelimiter, []byte("\t")) == 0 {
// double-escape
v = bytes.Replace(v, []byte("\t"), []byte("\\t"), -1)
} else if bytes.Contains(v, fielddelimiter) && !quoted {
// quote
var buf bytes.Buffer
buf.Write(quote)
buf.Write(v)
buf.Write(quote)
v = buf.Bytes()
}
return v
}
func passwordFromPgpass(user *user.User) (p string, err error) {
pgpassfilename := fmt.Sprintf("%s/.pgpass", user.HomeDir)
file, err := os.Open(pgpassfilename)
if err != nil {
return "", err
}
reader := csv.NewReader(file)
reader.Comma = ':'
reader.Comment = '#'
reader.TrimLeadingSpace = true
reader.FieldsPerRecord = 5
records, err := reader.ReadAll()
if err != nil {
return "", err
}
// Row format of pgpass file is "host:port:db:user:pass"
for _, record := range records {
if record[0] == hostname &&
record[1] == port &&
record[2] == dbname &&
record[3] == username {
return record[4], nil
}
}
return "", fmt.Errorf("Password for connection not found in %s", filename)
}
// display usage message
func usage() {
fmt.Fprintf(os.Stderr, "usage: pg2txt [flags]\n")
flag.PrintDefaults()
}
func exitWithError(err error) {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
// print application version
func version() {
fmt.Printf("v%s\n", VERSION)
}