/
main.go
291 lines (232 loc) · 6.44 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
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
package main
import (
"bufio"
"database/sql"
"encoding/csv"
"flag"
"fmt"
"io/ioutil"
"os"
"os/user"
"strconv"
"strings"
"time"
_ "github.com/lib/pq"
"github.com/psmithuk/xlsx"
)
var (
filename string
outputfile string
testconnection bool
command string
hostname string
port string
dbname string
username string
nopassword bool
columntitles bool
docuser string
showversion bool
)
const VERSION = "0.0.1"
func init() {
flag.StringVar(&filename, "f", "", "execute command from file (defaults to stdin)")
flag.StringVar(&outputfile, "o", "", "output file")
flag.BoolVar(&testconnection, "t", false, "test database connection and exit")
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.StringVar(&docuser, "propuser", "", "the username in the xlsx document properties (defaults to current login)")
flag.BoolVar(&showversion, "version", false, "print version string")
}
func main() {
var err error
flag.Usage = usage
flag.Parse()
if showversion {
version()
return
}
if outputfile == "" {
exitWithError(fmt.Errorf("You must specify an output file name"))
}
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))
sheetColumns := make([]xlsx.Column, len(columns))
for i, c := range columns {
sheetColumns[i] = xlsx.Column{Name: c, Width: 10}
}
sheet := xlsx.NewSheetWithColumns(sheetColumns)
// optionally add a row with column titles
if columntitles {
titleRow := sheet.NewRow()
for i, c := range columns {
titleRow.Cells[i] = xlsx.Cell{xlsx.CellTypeString, c}
}
sheet.AppendRow(titleRow)
}
// build the data rows
for rows.Next() {
sheetRow := sheet.NewRow()
for i, _ := range columns {
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)
for i, _ := range columns {
val := values[i]
sheetRow.Cells[i] = CellFromPostgres(val)
}
sheet.AppendRow(sheetRow)
}
err = sheet.SaveToFile(outputfile)
if err != nil {
exitWithError(fmt.Errorf("unable to get save xlsx sheet: %s", err))
}
}
// Convert a postgres value to a cell, inferring the cell format from the
// database/sql type returned by the pg driver
func CellFromPostgres(v interface{}) xlsx.Cell {
if v == nil {
return xlsx.Cell{xlsx.CellTypeString, ""}
}
switch v.(type) {
case ([]uint8):
s := string(v.([]uint8))
// string-like values prefixed with one or more
// zeroes are likely to be UPCs or other codes and should not
// be converted to numbers
if len(s) > 0 && (strings.Contains(s, ".") || s[0] != '0') {
_, err := strconv.ParseFloat(s, 10)
if err == nil {
return xlsx.Cell{xlsx.CellTypeNumber, s}
}
}
return xlsx.Cell{xlsx.CellTypeString, s}
case (bool):
if v.(bool) {
return xlsx.Cell{xlsx.CellTypeString, "Y"}
} else {
return xlsx.Cell{xlsx.CellTypeString, "N"}
}
case (int64):
return xlsx.Cell{xlsx.CellTypeNumber, fmt.Sprintf("%d", v)}
case (float64):
return xlsx.Cell{xlsx.CellTypeNumber, fmt.Sprintf("%f", v)}
case (time.Time):
return xlsx.Cell{xlsx.CellTypeDatetime, fmt.Sprintf("%s", v.(time.Time).Format(time.RFC3339))}
default:
return xlsx.Cell{xlsx.CellTypeString, fmt.Sprintf("%s", 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: pg2xlsx [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)
}