@@ -18,6 +18,7 @@ import (
1818 "github.com/mattn/go-isatty"
1919
2020 "github.com/sourcegraph/sourcegraph/lib/errors"
21+ "github.com/sourcegraph/sourcegraph/lib/output"
2122
2223 batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
2324 "github.com/sourcegraph/sourcegraph/lib/batches/template"
@@ -253,20 +254,32 @@ type executeBatchSpecOpts struct {
253254// executeBatchSpec performs all the steps required to upload the batch spec to
254255// Sourcegraph, including execution as needed and applying the resulting batch
255256// spec if specified.
256- func executeBatchSpec (ctx context.Context , ui ui.ExecUI , opts executeBatchSpecOpts ) (err error ) {
257+ func executeBatchSpec (ctx context.Context , opts executeBatchSpecOpts ) (err error ) {
258+ svc := service .New (& service.Opts {
259+ Client : opts .client ,
260+ })
261+
262+ ffs , err := svc .DetermineFeatureFlags (ctx )
263+ if err != nil {
264+ return err
265+ }
266+ var execUI ui.ExecUI
267+ if opts .flags .textOnly {
268+ execUI = & ui.JSONLines {BinaryDiffs : ffs .BinaryDiffs }
269+ } else {
270+ out := output .NewOutput (os .Stderr , output.OutputOpts {Verbose : * verbose })
271+ execUI = & ui.TUI {Out : out }
272+ }
273+
257274 defer func () {
258275 if err != nil {
259- ui .ExecutionError (err )
276+ execUI .ExecutionError (err )
260277 }
261278 }()
262279
263- svc := service .New (& service.Opts {
264- Client : opts .client ,
265- })
266-
267280 imageCache := docker .NewImageCache ()
268281
269- if err := validateSourcegraphVersionConstraint (ctx , svc ); err != nil {
282+ if err := validateSourcegraphVersionConstraint (ctx , ffs ); err != nil {
270283 return err
271284 }
272285
@@ -309,45 +322,45 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
309322 }
310323
311324 // Parse flags and build up our service and executor options.
312- ui .ParsingBatchSpec ()
325+ execUI .ParsingBatchSpec ()
313326 batchSpec , batchSpecDir , rawSpec , err := parseBatchSpec (ctx , opts .file , svc )
314327 if err != nil {
315328 var multiErr errors.MultiError
316329 if errors .As (err , & multiErr ) {
317- ui .ParsingBatchSpecFailure (multiErr )
330+ execUI .ParsingBatchSpecFailure (multiErr )
318331 return cmderrors .ExitCode (2 , nil )
319332 } else {
320333 // This shouldn't happen; let's just punt and let the normal
321334 // rendering occur.
322335 return err
323336 }
324337 }
325- ui .ParsingBatchSpecSuccess ()
338+ execUI .ParsingBatchSpecSuccess ()
326339
327- ui .ResolvingNamespace ()
340+ execUI .ResolvingNamespace ()
328341 namespace , err := svc .ResolveNamespace (ctx , opts .flags .namespace )
329342 if err != nil {
330343 return err
331344 }
332- ui .ResolvingNamespaceSuccess (namespace .ID )
345+ execUI .ResolvingNamespaceSuccess (namespace .ID )
333346
334347 var workspaceCreator workspace.Creator
335348
336349 if len (batchSpec .Steps ) > 0 {
337- ui .PreparingContainerImages ()
350+ execUI .PreparingContainerImages ()
338351 images , err := svc .EnsureDockerImages (
339352 ctx ,
340353 imageCache ,
341354 batchSpec .Steps ,
342355 parallelism ,
343- ui .PreparingContainerImagesProgress ,
356+ execUI .PreparingContainerImagesProgress ,
344357 )
345358 if err != nil {
346359 return err
347360 }
348- ui .PreparingContainerImagesSuccess ()
361+ execUI .PreparingContainerImagesSuccess ()
349362
350- ui .DeterminingWorkspaceCreatorType ()
363+ execUI .DeterminingWorkspaceCreatorType ()
351364 var typ workspace.CreatorType
352365 workspaceCreator , typ = workspace .NewCreator (ctx , opts .flags .workspace , opts .flags .cacheDir , opts .flags .tempDir , images )
353366 if typ == workspace .CreatorTypeVolume {
@@ -357,21 +370,21 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
357370 return err
358371 }
359372 }
360- ui .DeterminingWorkspaceCreatorTypeSuccess (typ )
373+ execUI .DeterminingWorkspaceCreatorTypeSuccess (typ )
361374 }
362375
363- ui .DeterminingWorkspaces ()
376+ execUI .DeterminingWorkspaces ()
364377 workspaces , repos , err := svc .ResolveWorkspacesForBatchSpec (ctx , batchSpec , opts .flags .allowUnsupported , opts .flags .allowIgnored )
365378 if err != nil {
366379 if repoSet , ok := err .(batches.UnsupportedRepoSet ); ok {
367- ui .DeterminingWorkspacesSuccess (len (workspaces ), len (repos ), repoSet , nil )
380+ execUI .DeterminingWorkspacesSuccess (len (workspaces ), len (repos ), repoSet , nil )
368381 } else if repoSet , ok := err .(batches.IgnoredRepoSet ); ok {
369- ui .DeterminingWorkspacesSuccess (len (workspaces ), len (repos ), nil , repoSet )
382+ execUI .DeterminingWorkspacesSuccess (len (workspaces ), len (repos ), nil , repoSet )
370383 } else {
371384 return errors .Wrap (err , "resolving repositories" )
372385 }
373386 } else {
374- ui .DeterminingWorkspacesSuccess (len (workspaces ), len (repos ), nil , nil )
387+ execUI .DeterminingWorkspacesSuccess (len (workspaces ), len (repos ), nil , nil )
375388 }
376389
377390 archiveRegistry := repozip .NewArchiveRegistry (opts .client , opts .flags .cacheDir , opts .flags .cleanArchives )
@@ -389,14 +402,16 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
389402 TempDir : opts .flags .tempDir ,
390403 GlobalEnv : os .Environ (),
391404 ForceRoot : opts .flags .runAsRoot ,
405+ BinaryDiffs : ffs .BinaryDiffs ,
392406 },
393- Logger : logManager ,
394- Cache : executor .NewDiskCache (opts .flags .cacheDir ),
395- GlobalEnv : os .Environ (),
407+ Logger : logManager ,
408+ Cache : executor .NewDiskCache (opts .flags .cacheDir ),
409+ BinaryDiffs : ffs .BinaryDiffs ,
410+ GlobalEnv : os .Environ (),
396411 },
397412 )
398413
399- ui .CheckingCache ()
414+ execUI .CheckingCache ()
400415 tasks := svc .BuildTasks (
401416 & template.BatchChangeAttributes {
402417 Name : batchSpec .Name ,
@@ -421,9 +436,9 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
421436 return err
422437 }
423438 }
424- ui .CheckingCacheSuccess (len (specs ), len (uncachedTasks ))
439+ execUI .CheckingCacheSuccess (len (specs ), len (uncachedTasks ))
425440
426- taskExecUI := ui .ExecutingTasks (* verbose , parallelism )
441+ taskExecUI := execUI .ExecutingTasks (* verbose , parallelism )
427442 freshSpecs , logFiles , execErr := coord .ExecuteAndBuildSpecs (ctx , batchSpec , uncachedTasks , taskExecUI )
428443 // Add external changeset specs.
429444 importedSpecs , importErr := svc .CreateImportChangesetSpecs (ctx , batchSpec )
@@ -440,7 +455,7 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
440455 if err == nil {
441456 taskExecUI .Success ()
442457 } else {
443- ui .ExecutingTasksSkippingErrors (err )
458+ execUI .ExecutingTasksSkippingErrors (err )
444459 }
445460 } else {
446461 if err != nil {
@@ -450,7 +465,7 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
450465 }
451466
452467 if len (logFiles ) > 0 && opts .flags .keepLogs {
453- ui .LogFilesKept (logFiles )
468+ execUI .LogFilesKept (logFiles )
454469 }
455470
456471 specs = append (specs , freshSpecs ... )
@@ -464,29 +479,29 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
464479 ids := make ([]graphql.ChangesetSpecID , len (specs ))
465480
466481 if len (specs ) > 0 {
467- ui .UploadingChangesetSpecs (len (specs ))
482+ execUI .UploadingChangesetSpecs (len (specs ))
468483
469484 for i , spec := range specs {
470485 id , err := svc .CreateChangesetSpec (ctx , spec )
471486 if err != nil {
472487 return err
473488 }
474489 ids [i ] = id
475- ui .UploadingChangesetSpecsProgress (i + 1 , len (specs ))
490+ execUI .UploadingChangesetSpecsProgress (i + 1 , len (specs ))
476491 }
477492
478- ui .UploadingChangesetSpecsSuccess (ids )
493+ execUI .UploadingChangesetSpecsSuccess (ids )
479494 } else if len (repos ) == 0 {
480- ui .NoChangesetSpecs ()
495+ execUI .NoChangesetSpecs ()
481496 }
482497
483- ui .CreatingBatchSpec ()
498+ execUI .CreatingBatchSpec ()
484499 id , url , err := svc .CreateBatchSpec (ctx , namespace .ID , rawSpec , ids )
485500 if err != nil {
486- return ui .CreatingBatchSpecError (err )
501+ return execUI .CreatingBatchSpecError (err )
487502 }
488503 previewURL := cfg .Endpoint + url
489- ui .CreatingBatchSpecSuccess (previewURL )
504+ execUI .CreatingBatchSpecSuccess (previewURL )
490505
491506 hasWorkspaceFiles := false
492507 for _ , step := range batchSpec .Steps {
@@ -496,26 +511,26 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
496511 }
497512 }
498513 if hasWorkspaceFiles {
499- ui .UploadingWorkspaceFiles ()
514+ execUI .UploadingWorkspaceFiles ()
500515 if err = svc .UploadBatchSpecWorkspaceFiles (ctx , batchSpecDir , string (id ), batchSpec .Steps ); err != nil {
501516 // Since failing to upload workspace files should not stop processing, just warn
502- ui .UploadingWorkspaceFilesWarning (errors .Wrap (err , "uploading workspace files" ))
517+ execUI .UploadingWorkspaceFilesWarning (errors .Wrap (err , "uploading workspace files" ))
503518 } else {
504- ui .UploadingWorkspaceFilesSuccess ()
519+ execUI .UploadingWorkspaceFilesSuccess ()
505520 }
506521 }
507522
508523 if ! opts .applyBatchSpec {
509- ui .PreviewBatchSpec (previewURL )
524+ execUI .PreviewBatchSpec (previewURL )
510525 return
511526 }
512527
513- ui .ApplyingBatchSpec ()
528+ execUI .ApplyingBatchSpec ()
514529 batch , err := svc .ApplyBatchChange (ctx , id )
515530 if err != nil {
516531 return err
517532 }
518- ui .ApplyingBatchSpecSuccess (cfg .Endpoint + batch .URL )
533+ execUI .ApplyingBatchSpecSuccess (cfg .Endpoint + batch .URL )
519534
520535 return nil
521536}
@@ -617,11 +632,7 @@ func getBatchParallelism(ctx context.Context, flag int) (int, error) {
617632 return docker .NCPU (ctx )
618633}
619634
620- func validateSourcegraphVersionConstraint (ctx context.Context , svc * service.Service ) error {
621- ffs , err := svc .DetermineFeatureFlags (ctx )
622- if err != nil {
623- return err
624- }
635+ func validateSourcegraphVersionConstraint (ctx context.Context , ffs * batches.FeatureFlags ) error {
625636 if ffs .Sourcegraph40 {
626637 return nil
627638 }
0 commit comments