Skip to content

Commit e9fa630

Browse files
authored
Clean up shared code between batch apply and preview (#517)
* Clean up shared code between `batch apply` and `preview` This is a follow-up to #511 and #512. It cleans up the code that's shared between `batch apply` and `previe` by trying to make the boundaries between the "TUI + execution"-glue-code, the `service` pkg, the `executor` pkg and the helpers clearer. A lot of this is subjective, of course (what does "clearer" even mean, man), but I think less duplication is Good and less cross-talk between a trio of components too. * Remove commented out code * Fix comment
1 parent b7ebb5f commit e9fa630

File tree

13 files changed

+195
-212
lines changed

13 files changed

+195
-212
lines changed

cmd/src/batch_apply.go

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"flag"
77
"fmt"
88

9-
"github.com/sourcegraph/src-cli/internal/batches/service"
109
"github.com/sourcegraph/src-cli/internal/output"
1110
)
1211

@@ -28,30 +27,7 @@ Examples:
2827
`
2928

3029
flagSet := flag.NewFlagSet("apply", flag.ExitOnError)
31-
flags := newBatchApplyFlags(flagSet, batchDefaultCacheDir(), batchDefaultTempDirPrefix())
32-
33-
doApply := func(ctx context.Context, out *output.Output, svc *service.Service, flags *batchApplyFlags) error {
34-
id, _, err := batchExecute(ctx, out, svc, flags)
35-
if err != nil {
36-
return err
37-
}
38-
39-
pending := batchCreatePending(out, "Applying batch spec")
40-
batch, err := svc.ApplyBatchChange(ctx, id)
41-
if err != nil {
42-
return err
43-
}
44-
batchCompletePending(pending, "Applying batch spec")
45-
46-
out.Write("")
47-
block := out.Block(output.Line(batchSuccessEmoji, batchSuccessColor, "Batch change applied!"))
48-
defer block.Close()
49-
50-
block.Write("To view the batch change, go to:")
51-
block.Writef("%s%s", cfg.Endpoint, batch.URL)
52-
53-
return nil
54-
}
30+
flags := newBatchExecuteFlags(flagSet, batchDefaultCacheDir(), batchDefaultTempDirPrefix())
5531

5632
handler := func(args []string) error {
5733
if err := flagSet.Parse(args); err != nil {
@@ -67,18 +43,14 @@ Examples:
6743
ctx, cancel := contextCancelOnInterrupt(context.Background())
6844
defer cancel()
6945

70-
svc := service.New(&service.Opts{
71-
AllowUnsupported: flags.allowUnsupported,
72-
AllowIgnored: flags.allowIgnored,
73-
Client: cfg.apiClient(flags.api, flagSet.Output()),
74-
Workspace: flags.workspace,
75-
})
46+
err := executeBatchSpec(ctx, executeBatchSpecOpts{
47+
flags: flags,
48+
out: out,
49+
client: cfg.apiClient(flags.api, flagSet.Output()),
7650

77-
if err := svc.DetermineFeatureFlags(ctx); err != nil {
78-
return err
79-
}
80-
81-
if err := doApply(ctx, out, svc, flags); err != nil {
51+
applyBatchSpec: true,
52+
})
53+
if err != nil {
8254
printExecutionError(out, err)
8355
out.Write("")
8456
return &exitCodeError{nil, 1}

0 commit comments

Comments
 (0)