Skip to content

Commit 26a9759

Browse files
authored
batches: Introduce ui package and implement task execution UI in TUI and JSONLines (#571)
This is part of https://github.com/sourcegraph/sourcegraph/issues/22766 and adds the missing task execution status updates in the `-text-only` output. It does so by defining the `executor.TaskExecutionUI` that the `Coordinator` and the `executor` use to let the UI know about the current state of the task execution. In order to make this happen I moved all of the UI-related things out of the `main` package and into a new, separate `batches/ui` package that contains the `ui.JSONLines` UI and the old TUI under a new name, `ui.TUI`. Both UIs can return a `TaskExecutionUI`: a `taskExecutionJSONLines` or `taskExecTUI`. The latter was formerly known as `batchProgressPrinter` and a mess of spaghetti code. Since the UIs are essentially event-driven it's much easier to implement a stateful UI such as `taskExecTUI`. It also feels quicker.
1 parent aab0599 commit 26a9759

32 files changed

+1334
-1335
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ All notable changes to `src-cli` are documented in this file.
1515

1616
### Changed
1717

18+
- For internal use only: `-text-only` now contains detailed information about task execution. [#571](https://github.com/sourcegraph/src-cli/pull/571)
19+
- As part of the above: the TUI of `src batch [preview|apply]` has been reworked and should now feel snappier.
20+
1821
### Fixed
1922

2023
### Removed

cmd/src/api.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package main
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76
"flag"
87
"fmt"
98
"io/ioutil"
109
"os"
1110
"strings"
1211

13-
"github.com/mattn/go-isatty"
1412
"github.com/sourcegraph/src-cli/internal/api"
13+
"github.com/sourcegraph/src-cli/internal/cmderrors"
14+
15+
"github.com/mattn/go-isatty"
1516
)
1617

1718
func init() {
@@ -65,7 +66,7 @@ Examples:
6566
if query == "" {
6667
// Read query from stdin instead.
6768
if isatty.IsTerminal(os.Stdin.Fd()) {
68-
return &usageError{errors.New("expected query to be piped into 'src api' or -query flag to be specified")}
69+
return cmderrors.Usage("expected query to be piped into 'src api' or -query flag to be specified")
6970
}
7071
data, err := ioutil.ReadAll(os.Stdin)
7172
if err != nil {
@@ -84,7 +85,7 @@ Examples:
8485
for _, arg := range flagSet.Args() {
8586
idx := strings.Index(arg, "=")
8687
if idx == -1 {
87-
return &usageError{fmt.Errorf("parsing argument %q expected 'variable=value' syntax (missing equals)", arg)}
88+
return cmderrors.Usagef("parsing argument %q expected 'variable=value' syntax (missing equals)", arg)
8889
}
8990
key := arg[:idx]
9091
value := arg[idx+1:]

cmd/src/batch_apply.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package main
22

33
import (
44
"context"
5-
"errors"
65
"flag"
76
"fmt"
87

98
"github.com/sourcegraph/sourcegraph/lib/output"
9+
"github.com/sourcegraph/src-cli/internal/batches/ui"
10+
"github.com/sourcegraph/src-cli/internal/cmderrors"
1011
)
1112

1213
func init() {
@@ -35,18 +36,18 @@ Examples:
3536
}
3637

3738
if len(flagSet.Args()) != 0 {
38-
return &usageError{errors.New("additional arguments not allowed")}
39+
return cmderrors.Usage("additional arguments not allowed")
3940
}
4041

4142
ctx, cancel := contextCancelOnInterrupt(context.Background())
4243
defer cancel()
4344

44-
var ui batchExecUI
45+
var execUI ui.ExecUI
4546
if flags.textOnly {
46-
ui = &batchExecJSONLinesUI{}
47+
execUI = &ui.JSONLines{}
4748
} else {
4849
out := output.NewOutput(flagSet.Output(), output.OutputOpts{Verbose: *verbose})
49-
ui = &batchExecTUI{out: out}
50+
execUI = &ui.TUI{Out: out}
5051
}
5152

5253
err := executeBatchSpec(ctx, executeBatchSpecOpts{
@@ -55,10 +56,10 @@ Examples:
5556

5657
applyBatchSpec: true,
5758

58-
ui: ui,
59+
ui: execUI,
5960
})
6061
if err != nil {
61-
return &exitCodeError{nil, 1}
62+
return cmderrors.ExitCode(1, nil)
6263
}
6364

6465
return nil

cmd/src/batch_common.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@ import (
1515

1616
"github.com/hashicorp/go-multierror"
1717
"github.com/pkg/errors"
18-
"github.com/sourcegraph/sourcegraph/lib/output"
1918
"github.com/sourcegraph/src-cli/internal/api"
2019
"github.com/sourcegraph/src-cli/internal/batches"
2120
"github.com/sourcegraph/src-cli/internal/batches/executor"
2221
"github.com/sourcegraph/src-cli/internal/batches/graphql"
2322
"github.com/sourcegraph/src-cli/internal/batches/service"
23+
"github.com/sourcegraph/src-cli/internal/batches/ui"
2424
"github.com/sourcegraph/src-cli/internal/batches/workspace"
25-
)
26-
27-
var (
28-
batchPendingColor = output.StylePending
29-
batchSuccessColor = output.StyleSuccess
30-
batchSuccessEmoji = output.EmojiSuccess
25+
"github.com/sourcegraph/src-cli/internal/cmderrors"
3126
)
3227

3328
type batchExecuteFlags struct {
@@ -125,14 +120,6 @@ func newBatchExecuteFlags(flagSet *flag.FlagSet, cacheDir, tempDir string) *batc
125120
return caf
126121
}
127122

128-
func batchCreatePending(out *output.Output, message string) output.Pending {
129-
return out.Pending(output.Line("", batchPendingColor, message))
130-
}
131-
132-
func batchCompletePending(p output.Pending, message string) {
133-
p.Complete(output.Line(batchSuccessEmoji, batchSuccessColor, message))
134-
}
135-
136123
func batchDefaultCacheDir() string {
137124
uc, err := os.UserCacheDir()
138125
if err != nil {
@@ -199,7 +186,7 @@ type executeBatchSpecOpts struct {
199186

200187
applyBatchSpec bool
201188

202-
ui batchExecUI
189+
ui ui.ExecUI
203190

204191
client api.Client
205192
}
@@ -238,10 +225,7 @@ func executeBatchSpec(ctx context.Context, opts executeBatchSpecOpts) (err error
238225
if err != nil {
239226
if merr, ok := err.(*multierror.Error); ok {
240227
opts.ui.ParsingBatchSpecFailure(merr)
241-
return &exitCodeError{
242-
error: nil,
243-
exitCode: 2,
244-
}
228+
return cmderrors.ExitCode(2, nil)
245229
} else {
246230
// This shouldn't happen; let's just punt and let the normal
247231
// rendering occur.
@@ -315,12 +299,12 @@ func executeBatchSpec(ctx context.Context, opts executeBatchSpecOpts) (err error
315299
}
316300
opts.ui.CheckingCacheSuccess(len(cachedSpecs), len(uncachedTasks))
317301

318-
printer := opts.ui.ExecutingTasks(*verbose, opts.flags.parallelism)
319-
freshSpecs, logFiles, err := coord.Execute(ctx, uncachedTasks, batchSpec, printer)
302+
taskExecUI := opts.ui.ExecutingTasks(*verbose, opts.flags.parallelism)
303+
freshSpecs, logFiles, err := coord.Execute(ctx, uncachedTasks, batchSpec, taskExecUI)
320304
if err != nil && !opts.flags.skipErrors {
321305
return err
322306
}
323-
opts.ui.ExecutingTasksSuccess()
307+
taskExecUI.Success()
324308
if err != nil && opts.flags.skipErrors {
325309
opts.ui.ExecutingTasksSkippingErrors(err)
326310
}

0 commit comments

Comments
 (0)