forked from sourcegraph/src-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
316 lines (277 loc) · 8.46 KB
/
api.go
File metadata and controls
316 lines (277 loc) · 8.46 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
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/kballard/go-shellquote"
"github.com/mattn/go-isatty"
)
func init() {
usage := `
Exit codes:
0: Success
1: General failures (connection issues, invalid HTTP response, etc.)
2: GraphQL error response
Examples:
Run queries (identical behavior):
$ echo 'query { currentUser { username } }' | src api
$ src api -query='query { currentUser { username } }'
Specify query variables:
$ echo '<query>' | src api 'var1=val1' 'var2=val2'
Searching for "Router" and getting result count:
$ echo 'query($query: String!) { search(query: $query) { results { resultCount } } }' | src api 'query=Router'
Get the curl command for a query (just add '-get-curl' in the flags section):
$ src api -get-curl -query='query { currentUser { username } }'
`
flagSet := flag.NewFlagSet("api", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
queryFlag = flagSet.String("query", "", "GraphQL query to execute, e.g. 'query { currentUser { username } }' (stdin otherwise)")
varsFlag = flagSet.String("vars", "", `GraphQL query variables to include as JSON string, e.g. '{"var": "val", "var2": "val2"}'`)
apiFlags = newAPIFlags(flagSet)
)
handler := func(args []string) error {
flagSet.Parse(args)
// Build the GraphQL request.
query := *queryFlag
if query == "" {
// Read query from stdin instead.
if isatty.IsTerminal(os.Stdin.Fd()) {
return &usageError{errors.New("expected query to be piped into 'src api' or -query flag to be specified")}
}
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
query = string(data)
}
// Determine which variables to use in the request.
vars := map[string]interface{}{}
if *varsFlag != "" {
if err := json.Unmarshal([]byte(*varsFlag), &vars); err != nil {
return err
}
}
for _, arg := range flagSet.Args() {
idx := strings.Index(arg, "=")
if idx == -1 {
return &usageError{fmt.Errorf("parsing argument %q expected 'variable=value' syntax (missing equals)", arg)}
}
key := arg[:idx]
value := arg[idx+1:]
vars[key] = value
}
// Perform the request.
var result interface{}
return (&apiRequest{
query: query,
vars: vars,
result: &result,
done: func() error {
// Print the formatted JSON.
f, err := marshalIndent(result)
if err != nil {
return err
}
fmt.Println(string(f))
return nil
},
flags: apiFlags,
dontUnpackErrors: true,
}).do()
}
// Register the command.
commands = append(commands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}
// gqlURL returns the URL to the GraphQL endpoint for the given Sourcegraph
// instance.
func gqlURL(endpoint string) string {
return endpoint + "/.api/graphql"
}
// curlCmd returns the curl command to perform the given GraphQL query. Bash-only.
func curlCmd(endpoint, accessToken, query string, vars map[string]interface{}) (string, error) {
data, err := json.Marshal(map[string]interface{}{
"query": query,
"variables": vars,
})
if err != nil {
return "", err
}
s := fmt.Sprintf("curl \\\n")
if accessToken != "" {
s += fmt.Sprintf(" %s \\\n", shellquote.Join("-H", "Authorization: token "+accessToken))
}
s += fmt.Sprintf(" %s \\\n", shellquote.Join("-d", string(data)))
s += fmt.Sprintf(" %s", shellquote.Join(gqlURL(endpoint)))
return s, nil
}
// apiRequest represents a GraphQL API request.
type apiRequest struct {
query string // the GraphQL query
vars map[string]interface{} // the GraphQL query variables
result interface{} // where to store the result
done func() error // a function to invoke for handling the response. If nil, flags like -get-curl are ignored.
flags *apiFlags // the API flags previously created via newAPIFlags
// If true, errors will not be unpacked.
//
// Consider a GraphQL response like:
//
// {"data": {...}, "errors": ["something went really wrong"]}
//
// 'error unpacking' refers to how we will check if there are any `errors`
// present in the response (if there are, we will report them on the command
// line separately AND exit with a proper error code), and if there are no
// errors `result` will contain only the `{...}` object.
//
// When true, the entire response object is stored in `result` -- as if you
// ran the curl query yourself.
dontUnpackErrors bool
}
// do performs the API request. If a.flags specify something like -get-curl
// then it is handled immediately and a.done is not invoked. Otherwise, once
// the request is finished a.done is invoked to handle the response (which is
// stored in a.result).
func (a *apiRequest) do() error {
if a.done != nil {
// Handle the get-curl flag now.
if *a.flags.getCurl {
curl, err := curlCmd(cfg.Endpoint, cfg.AccessToken, a.query, a.vars)
if err != nil {
return err
}
fmt.Println(curl)
return nil
}
} else {
a.done = func() error { return nil }
}
// Create the JSON object.
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(map[string]interface{}{
"query": a.query,
"variables": a.vars,
}); err != nil {
return err
}
// Create the HTTP request.
req, err := http.NewRequest("POST", gqlURL(cfg.Endpoint), nil)
if err != nil {
return err
}
if cfg.AccessToken != "" {
req.Header.Set("Authorization", "token "+cfg.AccessToken)
}
req.Body = ioutil.NopCloser(&buf)
// Perform the request.
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Our request may have failed before the reaching GraphQL endpoint, so
// confirm the status code. You can test this easily with e.g. an invalid
// endpoint like -endpoint=https://google.com
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusUnauthorized && isatty.IsCygwinTerminal(os.Stdout.Fd()) {
fmt.Println("You may need to specify or update your access token to use this endpoint.")
fmt.Println("See https://github.com/sourcegraph/src-cli#authentication")
fmt.Println("")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("error: %s\n\n%s", resp.Status, body)
}
// Decode the response.
var result struct {
Data interface{} `json:"data,omitempty"`
Errors interface{} `json:"errors,omitempty"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return err
}
// Handle the case of not unpacking errors.
if a.dontUnpackErrors {
if err := jsonCopy(a.result, result); err != nil {
return err
}
if err := a.done(); err != nil {
return err
}
if result.Errors != nil {
return &exitCodeError{error: nil, exitCode: graphqlErrorsExitCode}
}
return nil
}
// Handle the case of unpacking errors.
if result.Errors != nil {
return &exitCodeError{
error: fmt.Errorf("GraphQL errors:\n%s", &graphqlError{result.Errors}),
exitCode: graphqlErrorsExitCode,
}
}
if err := jsonCopy(a.result, result.Data); err != nil {
return err
}
return a.done()
}
// apiFlags represents generic API flags available in all commands that perform
// API requests. e.g. the ability to turn any CLI command into a curl command.
type apiFlags struct {
getCurl *bool
}
// newAPIFlags creates the API flags. It should be invoked once at flag setup
// time.
func newAPIFlags(flagSet *flag.FlagSet) *apiFlags {
return &apiFlags{
getCurl: flagSet.Bool("get-curl", false, "Print the curl command for executing this query and exit (WARNING: includes printing your access token!)"),
}
}
// jsonCopy is a cheaty method of copying an already-decoded JSON (src)
// response into its destination (dst) that would usually be passed to e.g.
// json.Unmarshal.
//
// We could do this with reflection, obviously, but it would be much more
// complex and JSON re-marshaling should be cheap enough anyway. Can improve in
// the future.
func jsonCopy(dst, src interface{}) error {
data, err := json.Marshal(src)
if err != nil {
return err
}
return json.NewDecoder(bytes.NewReader(data)).Decode(dst)
}
type graphqlError struct {
Errors interface{}
}
func (g *graphqlError) Error() string {
j, _ := marshalIndent(g.Errors)
return string(j)
}
func nullInt(n int) *int {
if n == -1 {
return nil
}
return &n
}
func nullString(s string) *string {
if s == "" {
return nil
}
return &s
}