/
main.go
183 lines (158 loc) · 4.12 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
package main
import (
"context"
"crypto/tls"
"encoding/hex"
"fmt"
"math/rand"
"net/url"
"os"
"runtime/debug"
"time"
"github.com/alecthomas/kong"
"github.com/apache/arrow/go/v14/arrow/flight"
"github.com/apache/arrow/go/v14/arrow/flight/flightsql"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)
const (
pgTimestampFormat = "2006-01-02 15:04:05.999999999"
traceIDHeader = "influx-trace-id"
traceIDHeader2 = "uber-trace-id"
)
// set by goreleaser
var version = "(devel)"
// Context is a CLI context.
type Context struct {
*CLI
}
// CLI contains the CLI parameters.
type CLI struct {
URL string `required:""`
DB string `required:""`
Token string `env:"FLIGHT_CLUB_TOKEN"`
Headers map[string]string `short:"H" env:"FLIGHT_CLUB_HEADERS"`
GenTraceId bool
Query QueryCmd `cmd:"" help:"query"`
Version kong.VersionFlag `name:"version" help:"Print version information and quit"`
}
type QueryCmd struct {
Query string `arg:"" help:"Query text"`
SkipWarmup bool `optional:"" help:"Skip warmup request"`
Output *os.File `short:"o" optional:"" help:"filename where output is printed"`
}
func (cmd *QueryCmd) Run(cli *Context) error {
ctx := metadata.AppendToOutgoingContext(context.Background(),
"database", cli.DB,
// we need to pass this explicitly because IOx doesn't support the `auth-token` header that flight passes
"authorization", "Token "+cli.Token,
// enables special queries
"iox-debug", "true",
)
ctx = metadata.AppendToOutgoingContext(ctx, cli.customHeaders()...)
if cli.GenTraceId {
traceID := generateRandomHex(8)
traceHeader := fmt.Sprintf("%s:1112223334445:0:1", traceID)
ctx = metadata.AppendToOutgoingContext(ctx,
traceIDHeader, traceHeader,
traceIDHeader2, traceHeader,
)
fmt.Fprintf(os.Stderr, "Trace ID set to %s\n", traceID)
}
addr, cred, err := parseAddr(cli.URL)
if err != nil {
return err
}
c, err := flightsql.NewClientCtx(ctx, addr, cli, nil, grpc.WithTransportCredentials(cred))
if err != nil {
return err
}
// Some time is spend on the first flight request, whatever that request is, let's run a dummy request first
// so that we can better measure the other ones
beforeWarmup := time.Now()
if !cmd.SkipWarmup {
if _, err := c.GetCatalogs(ctx); err != nil {
return err
}
}
warmupDuration := time.Since(beforeWarmup)
w := os.Stdout
if cmd.Output != nil {
w = cmd.Output
}
timings, err := printQuery(ctx, w, c, cmd.Query)
if err != nil {
return err
}
fmt.Println()
fmt.Print(timings.Add(Timings{Warmup: warmupDuration}))
return nil
}
func (cli *CLI) customHeaders() (pairs []string) {
for k, v := range cli.Headers {
pairs = append(pairs, k)
pairs = append(pairs, v)
}
return pairs
}
func (cli *CLI) Authenticate(context.Context, flight.AuthConn) error {
return fmt.Errorf("not implemented")
}
func (cli *CLI) GetToken(context.Context) (string, error) {
return cli.Token, nil
}
func parseAddr(s string) (string, credentials.TransportCredentials, error) {
u, err := url.Parse(s)
if err != nil {
return "", nil, err
}
p := u.Port()
if p == "" {
if u.Scheme == "https" {
p = "443"
} else if u.Scheme == "http" {
p = "80"
}
}
a := fmt.Sprintf("%s:%s", u.Hostname(), p)
switch u.Scheme {
case "http":
return a, insecure.NewCredentials(), nil
case "https":
return a, credentials.NewTLS(&tls.Config{}), nil
default:
return "", nil, fmt.Errorf("unhandled schema %q", u.Scheme)
}
}
func generateRandomHex(n int) string {
bytes := make([]byte, n)
rand.Seed(time.Now().UnixNano())
rand.Read(bytes)
return hex.EncodeToString(bytes)
}
func getVersion() string {
if bi, ok := debug.ReadBuildInfo(); ok {
if v := bi.Main.Version; v != "" && v != "(devel)" {
return v
}
}
// otherwise fallback to the version set by goreleaser
return version
}
func main() {
var cli CLI
ctx := kong.Parse(&cli,
kong.UsageOnError(),
kong.Vars{
"version": getVersion(),
},
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
Summary: true,
}),
)
err := ctx.Run(&Context{CLI: &cli})
ctx.FatalIfErrorf(err)
}