Skip to content

Commit 49463a4

Browse files
authored
api: add a standard -dump-requests flag (#266)
This flag dumps the request and response out to stdout for any request issued via the internal/api package. This is probably only useful to people developing src-cli, but it's also pretty lightweight.
1 parent d7d4bb2 commit 49463a4

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
1111
github.com/google/go-cmp v0.4.1
1212
github.com/hashicorp/go-multierror v1.1.0
13+
github.com/jig/teereadcloser v0.0.0-20181016160506-953720c48e05
1314
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
1415
github.com/mattn/go-colorable v0.1.6 // indirect
1516
github.com/mattn/go-isatty v0.0.12

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/U
1616
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
1717
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
1818
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
19+
github.com/jig/teereadcloser v0.0.0-20181016160506-953720c48e05 h1:dSwwtWuwMyarzsbVWOq4QJ8xVy9wgcNomvWyGtrKe+E=
20+
github.com/jig/teereadcloser v0.0.0-20181016160506-953720c48e05/go.mod h1:sRUFlj+HCejvoCRpuhU0EYnNw5FG+YJpz8UFfCf0F2U=
1921
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
2022
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
2123
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=

internal/api/api.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path"
1414

1515
"github.com/hashicorp/go-multierror"
16+
"github.com/jig/teereadcloser"
1617
"github.com/kballard/go-shellquote"
1718
"github.com/mattn/go-isatty"
1819
"github.com/pkg/errors"
@@ -138,6 +139,21 @@ func (r *request) do(ctx context.Context, result interface{}) (bool, error) {
138139
return false, nil
139140
}
140141

142+
if *r.client.opts.Flags.dump {
143+
fmt.Fprintf(r.client.opts.Out, "<-- query:\n%s\n\n", r.query)
144+
if len(r.vars) > 0 {
145+
fmt.Fprintln(r.client.opts.Out, "<-- variables:")
146+
for k, v := range r.vars {
147+
value, err := json.Marshal(v)
148+
if err != nil {
149+
return false, err
150+
}
151+
fmt.Fprintf(r.client.opts.Out, " %s: %s\n", k, string(value))
152+
}
153+
fmt.Fprintln(r.client.opts.Out, "")
154+
}
155+
}
156+
141157
// Create the JSON object.
142158
reqBody, err := json.Marshal(map[string]interface{}{
143159
"query": r.query,
@@ -181,8 +197,19 @@ func (r *request) do(ctx context.Context, result interface{}) (bool, error) {
181197
return false, fmt.Errorf("error: %s\n\n%s", resp.Status, body)
182198
}
183199

200+
body := resp.Body
201+
if *r.client.opts.Flags.dump {
202+
var buf bytes.Buffer
203+
body = ioaux.TeeReadCloser(resp.Body, &buf)
204+
defer func() {
205+
var out bytes.Buffer
206+
json.Indent(&out, buf.Bytes(), " ", " ")
207+
fmt.Fprintf(r.client.opts.Out, "--> %s\n\n", out.String())
208+
}()
209+
}
210+
184211
// Decode the response.
185-
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
212+
if err := json.NewDecoder(body).Decode(result); err != nil {
186213
return false, err
187214
}
188215

internal/api/flags.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "flag"
55
// Flags encapsulates the standard flags that should be added to all commands
66
// that issue API requests.
77
type Flags struct {
8+
dump *bool
89
getCurl *bool
910
trace *bool
1011
}
@@ -13,6 +14,7 @@ type Flags struct {
1314
// flag set.
1415
func NewFlags(flagSet *flag.FlagSet) *Flags {
1516
return &Flags{
17+
dump: flagSet.Bool("dump-requests", false, "Log GraphQL requests and responses to stdout"),
1618
getCurl: flagSet.Bool("get-curl", false, "Print the curl command for executing this query and exit (WARNING: includes printing your access token!)"),
1719
trace: flagSet.Bool("trace", false, "Log the trace ID for requests. See https://docs.sourcegraph.com/admin/observability/tracing"),
1820
}
@@ -21,6 +23,7 @@ func NewFlags(flagSet *flag.FlagSet) *Flags {
2123
func defaultFlags() *Flags {
2224
d := false
2325
return &Flags{
26+
dump: &d,
2427
getCurl: &d,
2528
trace: &d,
2629
}

0 commit comments

Comments
 (0)