-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathtui.go
More file actions
483 lines (398 loc) · 15.6 KB
/
Copy pathtui.go
File metadata and controls
483 lines (398 loc) · 15.6 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package ui
import (
"context"
"fmt"
"math"
"os/exec"
"github.com/neelance/parallel"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/output"
"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/batches"
"github.com/sourcegraph/src-cli/internal/batches/executor"
"github.com/sourcegraph/src-cli/internal/batches/graphql"
"github.com/sourcegraph/src-cli/internal/batches/workspace"
"github.com/sourcegraph/src-cli/internal/cmderrors"
)
var (
batchPendingColor = output.StylePending
batchSuccessColor = output.StyleSuccess
batchSuccessEmoji = output.EmojiSuccess
batchWarningColor = output.StyleWarning
batchWarningEmoji = output.EmojiWarning
)
var _ ExecUI = &TUI{}
type TUI struct {
Out *output.Output
pending output.Pending
progress output.Progress
progressPrinter *taskExecTUI
}
func (ui *TUI) ParsingBatchSpec() {
ui.pending = batchCreatePending(ui.Out, "Parsing batch spec")
}
func (ui *TUI) ParsingBatchSpecSuccess() {
batchCompletePending(ui.pending, "Parsing batch spec")
}
func (ui *TUI) ParsingBatchSpecFailure(err error) {
block := ui.Out.Block(output.Line("\u274c", output.StyleWarning, "Batch spec failed validation."))
defer block.Close()
var multiErr errors.MultiError
if errors.As(err, &multiErr) {
for i, err := range multiErr.Errors() {
block.Writef("%d. %s", i+1, err)
}
} else {
block.Writef("1. %s", err)
}
}
func (ui *TUI) ResolvingNamespace() {
ui.pending = batchCreatePending(ui.Out, "Resolving namespace")
}
func (ui *TUI) ResolvingNamespaceSuccess(namespace string) {
batchCompletePending(ui.pending, "Resolving namespace")
ui.Out.Verbosef("Resolved namespace: %s", namespace)
}
func (ui *TUI) PreparingContainerImages() {
ui.progress = ui.Out.Progress([]output.ProgressBar{{
Label: "Preparing container images",
Max: 1.0,
}}, nil)
}
func (ui *TUI) PreparingContainerImagesProgress(done, total int) {
ui.progress.SetValue(0, float64(done)/float64(total))
}
func (ui *TUI) PreparingContainerImagesSuccess() {
ui.progress.Complete()
ui.Out.Verbosef("Container images prepared successfully")
}
func (ui *TUI) DeterminingWorkspaceCreatorType() {
ui.pending = batchCreatePending(ui.Out, "Determining workspace type")
}
func (ui *TUI) DeterminingWorkspaceCreatorTypeSuccess(wt workspace.CreatorType) {
switch wt {
case workspace.CreatorTypeBind:
ui.pending.VerboseLine(output.Linef("🚧", output.StyleSuccess, "Workspace creator: bind"))
case workspace.CreatorTypeVolume:
ui.pending.VerboseLine(output.Linef("🚧", output.StyleSuccess, "Workspace creator: volume"))
}
batchCompletePending(ui.pending, "Set workspace type")
}
func (ui *TUI) DeterminingWorkspaces() {
ui.pending = batchCreatePending(ui.Out, "Determining workspaces")
}
func (ui *TUI) DeterminingWorkspacesSuccess(workspacesCount, reposCount int, unsupported batches.UnsupportedRepoSet, ignored batches.IgnoredRepoSet) {
batchCompletePending(ui.pending, fmt.Sprintf("Resolved %d workspaces from %d repositories", workspacesCount, reposCount))
ui.Out.Verbosef("Workspace resolution: %d workspaces across %d repositories", workspacesCount, reposCount)
if len(unsupported) != 0 {
ui.Out.Verbosef("Unsupported repositories: %d", len(unsupported))
block := ui.Out.Block(output.Line(" ", output.StyleWarning, "Some repositories are hosted on unsupported code hosts and will be skipped. Use the -allow-unsupported flag to avoid skipping them."))
for repo := range unsupported {
block.Write(repo.Name)
}
block.Close()
} else if len(ignored) != 0 {
ui.Out.Verbosef("Ignored repositories (have .batchignore): %d", len(ignored))
block := ui.Out.Block(output.Line(" ", output.StyleWarning, "The repositories listed below contain .batchignore files and will be skipped. Use the -force-override-ignore flag to avoid skipping them."))
for repo := range ignored {
block.Write(repo.Name)
}
block.Close()
}
ui.maybeWorkspaceCountWarning(workspacesCount, 500)
}
func (ui *TUI) maybeWorkspaceCountWarning(count, limit int) {
if count > limit {
block := ui.Out.Block(output.Linef(
"⚠️", output.StyleWarning,
"Batch changes with more than %d workspaces may be unwieldy to manage.",
limit,
))
for _, line := range []string{
"We're working on providing more filtering options, and you can continue with",
fmt.Sprintf(
"this batch change if you want, but you may want to break it into %d or more",
int(math.Ceil(float64(count)/float64(limit))),
),
"batch changes if you can.",
} {
block.WriteLine(output.Line("", output.StyleSuggestion, line))
}
block.Close()
}
}
func (ui *TUI) CheckingCache() {
ui.pending = batchCreatePending(ui.Out, "Checking cache for changeset specs")
}
func (ui *TUI) CheckingCacheSuccess(cachedSpecsFound int, uncachedTasks int) {
var specsFoundMessage string
if cachedSpecsFound == 1 {
specsFoundMessage = "Found 1 cached changeset spec"
} else {
specsFoundMessage = fmt.Sprintf("Found %d cached changeset specs", cachedSpecsFound)
}
switch uncachedTasks {
case 0:
batchCompletePending(ui.pending, fmt.Sprintf("%s; no tasks need to be executed", specsFoundMessage))
case 1:
batchCompletePending(ui.pending, fmt.Sprintf("%s; %d task needs to be executed", specsFoundMessage, uncachedTasks))
default:
batchCompletePending(ui.pending, fmt.Sprintf("%s; %d tasks need to be executed", specsFoundMessage, uncachedTasks))
}
ui.Out.Verbosef("Cache check: %d cached specs found, %d tasks to execute", cachedSpecsFound, uncachedTasks)
}
func (ui *TUI) ExecutingTasks(verbose bool, parallelism int) executor.TaskExecutionUI {
ui.progressPrinter = newTaskExecTUI(ui.Out, verbose, parallelism)
return ui.progressPrinter
}
func (ui *TUI) ExecutingTasksSkippingErrors(err error) {
printExecutionError(ui.Out, err)
ui.Out.WriteLine(output.Line(output.EmojiWarning, output.StyleWarning, "Skipping errors because -skip-errors was used."))
}
func (ui *TUI) LogFilesKept(files []string) {
block := ui.Out.Block(output.Line("", batchSuccessColor, "Preserving log files:"))
defer block.Close()
for _, file := range files {
block.Write(file)
}
}
func (ui *TUI) NoChangesetSpecs() {
ui.Out.WriteLine(output.Linef(output.EmojiWarning, output.StyleWarning, `No changeset specs created`))
}
func (ui *TUI) UploadingChangesetSpecs(num int) {
var label string
if num == 1 {
label = "Sending changeset spec"
} else {
label = fmt.Sprintf("Sending %d changeset specs", num)
}
ui.progress = ui.Out.Progress([]output.ProgressBar{
{Label: label, Max: float64(num)},
}, nil)
}
func (ui *TUI) UploadingChangesetSpecsProgress(done, total int) {
ui.progress.SetValue(0, float64(done))
}
func (ui *TUI) UploadingChangesetSpecsSuccess(ids []graphql.ChangesetSpecID) {
ui.progress.Complete()
}
func (ui *TUI) CreatingBatchSpec() {
ui.pending = batchCreatePending(ui.Out, "Creating batch spec on Sourcegraph")
}
func (ui *TUI) CreatingBatchSpecSuccess(previewURL string) {
batchCompletePending(ui.pending, "Creating batch spec on Sourcegraph")
}
func (ui *TUI) CreatingBatchSpecError(maxUnlicensedCS int, err error) error {
return prettyPrintBatchUnlicensedError(ui.Out, maxUnlicensedCS, err)
}
func (ui *TUI) DockerWatchDogWarning(err error) {
dockerWatchDogWarning(ui.Out, err)
}
func (ui *TUI) PreviewBatchSpec(batchSpecURL string) {
ui.Out.Write("")
block := ui.Out.Block(output.Line(batchSuccessEmoji, batchSuccessColor, "To preview or apply the batch spec, go to:"))
defer block.Close()
block.Writef("%s", batchSpecURL)
}
func (ui *TUI) ApplyingBatchSpec() {
ui.pending = batchCreatePending(ui.Out, "Applying batch spec")
}
func (ui *TUI) ApplyingBatchSpecSuccess(batchChangeURL string) {
batchCompletePending(ui.pending, "Applying batch spec")
ui.Out.Write("")
block := ui.Out.Block(output.Line(batchSuccessEmoji, batchSuccessColor, "Batch change applied!"))
defer block.Close()
block.Write("To view the batch change, go to:")
block.Writef("%s", batchChangeURL)
}
func (ui *TUI) SendingBatchChange() {
ui.pending = batchCreatePending(ui.Out, "Sending batch change")
}
func (ui *TUI) SendingBatchChangeSuccess() {
batchCompletePending(ui.pending, "Sending batch change")
}
func (ui *TUI) SendingBatchSpec() {
ui.pending = batchCreatePending(ui.Out, "Sending batch spec")
}
func (ui *TUI) SendingBatchSpecSuccess() {
batchCompletePending(ui.pending, "Sending batch spec")
}
func (ui *TUI) UploadingWorkspaceFiles() {
ui.pending = batchCreatePending(ui.Out, "Uploading workspace files")
}
func (ui *TUI) UploadingWorkspaceFilesWarning(err error) {
batchCompleteWarning(ui.pending, err.Error())
}
func (ui *TUI) UploadingWorkspaceFilesSuccess() {
batchCompletePending(ui.pending, "Uploading workspace files")
}
func (ui *TUI) ResolvingWorkspaces() {
ui.pending = batchCreatePending(ui.Out, "Resolving workspaces")
}
func (ui *TUI) ResolvingWorkspacesSuccess(workspacesCount int) {
batchCompletePending(ui.pending, "Resolving workspaces")
ui.maybeWorkspaceCountWarning(workspacesCount, 2000)
}
func (ui *TUI) ExecutingBatchSpec() {
ui.pending = batchCreatePending(ui.Out, "Executing batch spec")
}
func (ui *TUI) ExecutingBatchSpecSuccess() {
batchCompletePending(ui.pending, "Executing batch spec")
}
func (ui *TUI) ExecutionError(err error) {
printExecutionError(ui.Out, err)
}
func (ui *TUI) RemoteSuccess(url string) {
ui.Out.WriteLine(output.Line(output.EmojiLightbulb, output.Fg256Color(12), "Executing at: "+url))
}
// prettyPrintBatchUnlicensedError introspects the given error returned when
// creating a batch spec and ascertains whether it's a licensing error. If it
// is, then a better message is output. Regardless, the return value of this
// function should be used to replace the original error passed in to ensure
// that the displayed output is sensible.
func prettyPrintBatchUnlicensedError(out *output.Output, maxUnlicensedCS int, err error) error {
// Pull apart the error to see if it's a licensing error: if so, we should
// display a friendlier and more actionable message than the usual GraphQL
// error output.
var gerrs api.GraphQlErrors
if errors.As(err, &gerrs) {
// A licensing error should be the sole error returned, so we'll only
// pretty print if there's one error.
if len(gerrs) == 1 {
if code, cerr := gerrs[0].Code(); cerr != nil {
// We got a malformed value in the error extensions; at this
// point, there's not much sensible we can do. Let's log this in
// verbose mode, but let the original error bubble up rather
// than this one.
out.Verbosef("Unexpected error parsing the GraphQL error: %v", cerr)
} else if code == "ErrBatchChangesUnlicensed" || code == "ErrBatchChangesOverLimit" {
// OK, let's print a better message, then return an
// exitCodeError to suppress the normal automatic error block.
// Note that we have hand wrapped the output at 80 (printable)
// characters: having automatic wrapping some day would be nice,
// but this should be sufficient for now.
block := out.Block(output.Line("🪙", output.StyleWarning, "Batch Changes is a paid feature of Sourcegraph. All users can create sample"))
block.WriteLine(output.Linef("", output.StyleWarning, "batch changes with up to %v changesets without a license. Contact Sourcegraph", maxUnlicensedCS))
block.WriteLine(output.Linef("", output.StyleWarning, "sales at %shttps://about.sourcegraph.com/contact/sales/%s to obtain a trial", output.StyleSearchLink, output.StyleWarning))
block.WriteLine(output.Linef("", output.StyleWarning, "license."))
block.Write("")
block.WriteLine(output.Linef("", output.StyleWarning, "To proceed with this batch change, you will need to create %v or fewer", maxUnlicensedCS))
block.WriteLine(output.Linef("", output.StyleWarning, "changesets. To do so, you could try adding %scount:%v%s to your", output.StyleSearchAlertProposedQuery, maxUnlicensedCS, output.StyleWarning))
block.WriteLine(output.Linef("", output.StyleWarning, "%srepositoriesMatchingQuery%s search, or reduce the number of changesets in", output.StyleReset, output.StyleWarning))
block.WriteLine(output.Linef("", output.StyleWarning, "%simportChangesets%s.", output.StyleReset, output.StyleWarning))
block.Close()
return cmderrors.ExitCode(cmderrors.GraphqlErrorsExitCode, nil)
}
}
}
// In all other cases, we'll just return the original error.
return err
}
// printExecutionError is used to print the possible error returned by
// batchExecute.
func printExecutionError(out *output.Output, err error) {
// exitCodeError shouldn't generate any specific output, since it indicates
// that this was done deeper in the call stack.
var exitCodeErr *cmderrors.ExitCodeError
if errors.As(err, &exitCodeErr) {
return
}
out.Write("")
writeErrs := func(errs []error) {
var block *output.Block
if len(errs) > 1 {
block = out.Block(output.Linef(output.EmojiFailure, output.StyleWarning, "%d errors:", len(errs)))
} else {
block = out.Block(output.Line(output.EmojiFailure, output.StyleWarning, "Error:"))
}
for _, e := range errs {
var taskErr executor.TaskExecutionErr
if errors.As(e, &taskErr) {
block.Write(formatTaskExecutionErr(taskErr))
} else {
if errors.Is(err, context.Canceled) {
block.Writef("%sAborting", output.StyleBold)
} else {
block.Writef("%s%s", output.StyleBold, e.Error())
}
}
}
if block != nil {
block.Close()
}
}
var (
parErrs parallel.Errors
multiErr errors.MultiError
gqlErrs api.GraphQlErrors
)
switch {
case errors.As(err, &parErrs), errors.As(err, &multiErr), errors.As(err, &gqlErrs):
writeErrs(flattenErrs(err))
default:
writeErrs([]error{err})
}
out.Write("")
block := out.Block(output.Line(output.EmojiLightbulb, output.StyleSuggestion, "The troubleshooting documentation can help to narrow down the cause of the errors:"))
block.WriteLine(output.Line("", output.StyleSuggestion, "https://docs.sourcegraph.com/batch_changes/references/troubleshooting"))
block.Close()
}
func flattenErrs(err error) (result []error) {
var (
parErrs parallel.Errors
multiErr errors.MultiError
gqlErrs api.GraphQlErrors
)
switch {
case errors.As(err, &parErrs):
for _, e := range parErrs {
result = append(result, flattenErrs(e)...)
}
case errors.As(err, &multiErr):
for _, e := range multiErr.Errors() {
result = append(result, flattenErrs(e)...)
}
case errors.As(err, &gqlErrs):
for _, e := range gqlErrs {
result = append(result, flattenErrs(e)...)
}
default:
result = append(result, err)
}
return result
}
func formatTaskExecutionErr(err executor.TaskExecutionErr) string {
var ee *exec.ExitError
if errors.As(err, &ee) && ee.String() == "signal: killed" {
return fmt.Sprintf(
"%s%s%s: killed by interrupt signal",
output.StyleBold,
err.Repository,
output.StyleReset,
)
}
return fmt.Sprintf(
"%s%s%s:\n%s\nLog: %s\n",
output.StyleBold,
err.Repository,
output.StyleReset,
err.Err,
err.Logfile,
)
}
func batchCreatePending(out *output.Output, message string) output.Pending {
return out.Pending(output.Line("", batchPendingColor, message))
}
func batchCompletePending(p output.Pending, message string) {
p.Complete(output.Line(batchSuccessEmoji, batchSuccessColor, message))
}
func batchCompleteWarning(p output.Pending, message string) {
p.Complete(output.Line(batchWarningEmoji, batchWarningColor, message))
}
func dockerWatchDogWarning(out *output.Output, err error) {
block := out.Block(output.Line("🐳", output.StyleWarning, "It seems your Docker engine might be frozen."))
block.WriteLine(output.Line("", output.StyleWarning, "If there's no progress in the next couple minutes, you may want to try restarting Docker and running the command again."))
block.WriteLine(output.Linef("", output.StyleWarning, "Error: %s", err.Error()))
block.Write("")
block.Close()
}