-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathrepos_list.go
More file actions
259 lines (226 loc) · 6.84 KB
/
repos_list.go
File metadata and controls
259 lines (226 loc) · 6.84 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"strings"
"github.com/sourcegraph/src-cli/internal/api"
)
type reposListOptions struct {
first int
query string
cloned bool
notCloned bool
indexed bool
notIndexed bool
orderBy string
descending bool
}
type repositoriesListResult struct {
Data struct {
Repositories struct {
Nodes []Repository `json:"nodes"`
} `json:"repositories"`
} `json:"data"`
Errors []json.RawMessage `json:"errors,omitempty"`
}
// listRepositories returns the repositories from the response, any GraphQL
// errors returned alongside data (should be treated as warnings), and
// a hard error when the query fails without usable repository data.
func listRepositories(ctx context.Context, client api.Client, params reposListOptions) ([]Repository, api.GraphQlErrors, error) {
query := `query Repositories(
$first: Int,
$query: String,
$cloned: Boolean,
$notCloned: Boolean,
$indexed: Boolean,
$notIndexed: Boolean,
$orderBy: RepositoryOrderBy,
$descending: Boolean,
) {
repositories(
first: $first,
query: $query,
cloned: $cloned,
notCloned: $notCloned,
indexed: $indexed,
notIndexed: $notIndexed,
orderBy: $orderBy,
descending: $descending,
) {
nodes {
...RepositoryFields
}
}
}
` + repositoryFragment
var result repositoriesListResult
ok, err := client.NewRequest(query, map[string]any{
"first": api.NullInt(params.first),
"query": api.NullString(params.query),
"cloned": params.cloned,
"notCloned": params.notCloned,
"indexed": params.indexed,
"notIndexed": params.notIndexed,
"orderBy": params.orderBy,
"descending": params.descending,
}).DoRaw(ctx, &result)
if err != nil || !ok {
return nil, nil, err
}
repos := result.Data.Repositories.Nodes
if len(result.Errors) == 0 {
return repos, nil, nil
}
errors := api.NewGraphQlErrors(result.Errors)
if len(repos) > 0 {
return repos, errors, nil
}
return nil, nil, errors
}
func gqlErrorPathString(pathSegment any) (string, bool) {
value, ok := pathSegment.(string)
return value, ok
}
func gqlErrorIndex(pathSegment any) (int, bool) {
switch value := pathSegment.(type) {
case float64:
index := int(value)
return index, float64(index) == value && index >= 0
case int:
return value, value >= 0
default:
return 0, false
}
}
func gqlWarningPath(graphQLError *api.GraphQlError) string {
path, err := graphQLError.Path()
if err != nil || len(path) == 0 {
return ""
}
var b strings.Builder
for _, pathSegment := range path {
if segment, ok := gqlErrorPathString(pathSegment); ok {
if b.Len() > 0 {
b.WriteByte('.')
}
b.WriteString(segment)
continue
}
if index, ok := gqlErrorIndex(pathSegment); ok {
fmt.Fprintf(&b, "[%d]", index)
}
}
return b.String()
}
func gqlWarningMessage(graphQLError *api.GraphQlError) string {
message, err := graphQLError.Message()
if err != nil || message == "" {
return graphQLError.Error()
}
return message
}
func formatRepositoryListWarnings(warnings api.GraphQlErrors) string {
var b strings.Builder
fmt.Fprintf(&b, "warnings: %d errors during listing\n", len(warnings))
for _, warning := range warnings {
path := gqlWarningPath(warning)
message := gqlWarningMessage(warning)
if path != "" {
fmt.Fprintf(&b, "%s - %s\n", path, message)
} else {
fmt.Fprintf(&b, "%s\n", message)
}
fmt.Fprintf(&b, "%s\n", warning.Error())
}
return b.String()
}
func init() {
usage := `
Examples:
List repositories:
$ src repos list
Print JSON description of repositories list:
$ src repos list -f '{{.|json}}'
List repositories whose names match the query:
$ src repos list -query='myquery'
`
flagSet := flag.NewFlagSet("list", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src repos %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
firstFlag = flagSet.Int("first", 1000, "Returns the first n repositories from the list.")
queryFlag = flagSet.String("query", "", `Returns repositories whose names match the query. (e.g. "myorg/")`)
// TODO: add support for "names" field.
clonedFlag = flagSet.Bool("cloned", true, "Include cloned repositories.")
notClonedFlag = flagSet.Bool("not-cloned", true, "Include repositories that are not yet cloned and for which cloning is not in progress.")
indexedFlag = flagSet.Bool("indexed", true, "Include repositories that have a text search index.")
notIndexedFlag = flagSet.Bool("not-indexed", true, "Include repositories that do not have a text search index.")
orderByFlag = flagSet.String("order-by", "name", `How to order the results; possible choices are: "name", "created-at"`)
descendingFlag = flagSet.Bool("descending", false, "Whether or not results should be in descending order.")
namesWithoutHostFlag = flagSet.Bool("names-without-host", false, "Whether or not repository names should be printed without the hostname (or other first path component). If set, -f is ignored.")
formatFlag = flagSet.String("f", "{{.Name}}", `Format for the output, using the syntax of Go package text/template. (e.g. "{{.ID}}: {{.Name}}") or "{{.|json}}")`)
apiFlags = api.NewFlags(flagSet)
)
handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
client := cfg.apiClient(apiFlags, flagSet.Output())
tmpl, err := parseTemplate(*formatFlag)
if err != nil {
return err
}
var orderBy string
switch *orderByFlag {
case "name":
orderBy = "REPOSITORY_NAME"
case "created-at":
orderBy = "REPO_CREATED_AT"
default:
return fmt.Errorf("invalid -order-by flag value: %q", *orderByFlag)
}
// if we get repos and errors during a listing, we consider the errors as warnings and the data partially complete
repos, warnings, err := listRepositories(context.Background(), client, reposListOptions{
first: *firstFlag,
query: *queryFlag,
cloned: *clonedFlag,
notCloned: *notClonedFlag,
indexed: *indexedFlag,
notIndexed: *notIndexedFlag,
orderBy: orderBy,
descending: *descendingFlag,
})
if err != nil {
return err
}
for _, repo := range repos {
if *namesWithoutHostFlag {
firstSlash := strings.Index(repo.Name, "/")
fmt.Println(repo.Name[firstSlash+len("/"):])
continue
}
if err := execTemplate(tmpl, repo); err != nil {
return err
}
}
if len(warnings) > 0 {
if *verbose {
fmt.Fprint(flagSet.Output(), formatRepositoryListWarnings(warnings))
} else {
fmt.Fprintf(flagSet.Output(), "warning: %d errors during listing; rerun with -v to inspect them\n", len(warnings))
}
}
return nil
}
// Register the command.
reposCommands = append(reposCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}