-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathapi.go
More file actions
111 lines (93 loc) · 2.91 KB
/
Copy pathapi.go
File metadata and controls
111 lines (93 loc) · 2.91 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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/sourcegraph/src-cli/internal/clicompat"
"github.com/sourcegraph/src-cli/internal/cmderrors"
"github.com/mattn/go-isatty"
"github.com/urfave/cli/v3"
)
const apiExamples = `
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 } }'
`
var apiCommand = clicompat.Wrap(&cli.Command{
Name: "api",
Usage: "interacts with the Sourcegraph GraphQL API",
UsageText: "src api [options] [variable=value ...]",
Description: apiExamples,
HideVersion: true,
DisableSliceFlagSeparator: true,
Flags: clicompat.WithAPIFlags(
&cli.StringFlag{
Name: "query",
Usage: "GraphQL query to execute, e.g. 'query { currentUser { username } }' (stdin otherwise)",
},
&cli.StringFlag{
Name: "vars",
Usage: `GraphQL query variables to include as JSON string, e.g. '{"var": "val", "var2": "val2"}'`,
},
),
Action: func(ctx context.Context, cmd *cli.Command) error {
query := cmd.String("query")
if query == "" {
if isatty.IsTerminal(os.Stdin.Fd()) {
return cmderrors.Usage("expected query to be piped into 'src api' or -query flag to be specified")
}
data, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
query = string(data)
}
vars := map[string]any{}
if raw := cmd.String("vars"); raw != "" {
if err := json.Unmarshal([]byte(raw), &vars); err != nil {
return err
}
}
for _, arg := range cmd.Args().Slice() {
key, value, ok := strings.Cut(arg, "=")
if !ok {
return cmderrors.Usagef("parsing argument %q expected 'variable=value' syntax (missing equals)", arg)
}
vars[key] = value
}
var result struct {
Data any `json:"data,omitempty"`
Errors []json.RawMessage `json:"errors,omitempty"`
}
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
if ok, err := client.NewRequest(query, vars).DoRaw(ctx, &result); err != nil || !ok {
return err
}
formatted, err := marshalIndent(result)
if err != nil {
return err
}
_, err = fmt.Fprintln(cmd.Writer, string(formatted))
if err != nil {
return err
}
if len(result.Errors) > 0 {
return cmderrors.ExitCode(cmderrors.GraphqlErrorsExitCode, nil)
}
return nil
},
})