Skip to content

Commit 4d5ee4b

Browse files
authored
refactor(internal): pass context through to command.Run (#3192)
`command.Run` now takes a `context.Context`, so callers can cancel work in subprocesses. This makes it so that in parallel flows subprocesses will stop as soon as the context is cancelled, instead of continuing to run in the background.
1 parent 7848b16 commit 4d5ee4b

27 files changed

+147
-141
lines changed

internal/command/command.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
package command
1717

1818
import (
19+
"context"
1920
"fmt"
2021
"os"
2122
"os/exec"
2223
)
2324

2425
// Run executes a program (with arguments) and captures any error output.
25-
func Run(command string, arg ...string) error {
26-
cmd := exec.Command(command, arg...)
26+
func Run(ctx context.Context, command string, arg ...string) error {
27+
cmd := exec.CommandContext(ctx, command, arg...)
2728
fmt.Fprintf(os.Stderr, "Running: %s\n", cmd.String())
2829
if output, err := cmd.CombinedOutput(); err != nil {
2930
return fmt.Errorf("%v: %v\n%s", cmd, err, output)

internal/command/command_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import (
2020
)
2121

2222
func TestRun(t *testing.T) {
23-
if err := Run("go", "version"); err != nil {
23+
if err := Run(t.Context(), "go", "version"); err != nil {
2424
t.Fatal(err)
2525
}
2626
}
2727

2828
func TestRunError(t *testing.T) {
29-
err := Run("go", "invalid-subcommand-bad-bad-bad")
29+
err := Run(t.Context(), "go", "invalid-subcommand-bad-bad-bad")
3030
if err == nil {
3131
t.Fatal("expected error, got nil")
3232
}

internal/librarian/generate.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func runGenerate(ctx context.Context, all bool, libraryName string) error {
7979
if err != nil {
8080
return err
8181
}
82-
return formatLibrary(cfg.Language, lib)
82+
return formatLibrary(ctx, cfg.Language, lib)
8383
}
8484

8585
func generateAll(ctx context.Context, cfg *config.Config) error {
@@ -98,7 +98,7 @@ func generateAll(ctx context.Context, cfg *config.Config) error {
9898
if err != nil {
9999
return err
100100
}
101-
if err := formatLibrary(cfg.Language, lib); err != nil {
101+
if err := formatLibrary(ctx, cfg.Language, lib); err != nil {
102102
return err
103103
}
104104
}
@@ -253,12 +253,12 @@ func generate(ctx context.Context, language string, library *config.Library, sou
253253
return library, nil
254254
}
255255

256-
func formatLibrary(language string, library *config.Library) error {
256+
func formatLibrary(ctx context.Context, language string, library *config.Library) error {
257257
switch language {
258258
case "testhelper":
259259
return nil
260260
case "rust":
261-
return rust.Format(library)
261+
return rust.Format(ctx, library)
262262
}
263263
return fmt.Errorf("format not implemented for %q", language)
264264
}

internal/librarian/internal/rust/generate.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func Generate(ctx context.Context, library *config.Library, sources *config.Sour
4141
return err
4242
}
4343
if library.Veneer {
44-
return generateVeneer(library, googleapisDir)
44+
return generateVeneer(ctx, library, googleapisDir)
4545
}
4646

4747
if len(library.Channels) != 1 {
@@ -57,7 +57,7 @@ func Generate(ctx context.Context, library *config.Library, sources *config.Sour
5757
if err != nil {
5858
return err
5959
}
60-
if err := sidekickrust.Generate(model, library.Output, sidekickConfig); err != nil {
60+
if err := sidekickrust.Generate(ctx, model, library.Output, sidekickConfig); err != nil {
6161
return err
6262
}
6363
return nil
@@ -66,17 +66,17 @@ func Generate(ctx context.Context, library *config.Library, sources *config.Sour
6666
// Format formats a generated Rust library. Must be called sequentially;
6767
// parallel calls cause race conditions as cargo fmt runs cargo metadata,
6868
// which competes for locks on the workspace Cargo.toml and Cargo.lock.
69-
func Format(library *config.Library) error {
70-
if err := command.Run("taplo", "fmt", filepath.Join(library.Output, "Cargo.toml")); err != nil {
69+
func Format(ctx context.Context, library *config.Library) error {
70+
if err := command.Run(ctx, "taplo", "fmt", filepath.Join(library.Output, "Cargo.toml")); err != nil {
7171
return err
7272
}
73-
if err := command.Run("cargo", "fmt", "-p", library.Name); err != nil {
73+
if err := command.Run(ctx, "cargo", "fmt", "-p", library.Name); err != nil {
7474
return err
7575
}
7676
return nil
7777
}
7878

79-
func generateVeneer(library *config.Library, googleapisDir string) error {
79+
func generateVeneer(ctx context.Context, library *config.Library, googleapisDir string) error {
8080
if library.Rust == nil || len(library.Rust.Modules) == 0 {
8181
return fmt.Errorf("veneer %q has no modules defined", library.Name)
8282
}
@@ -88,9 +88,9 @@ func generateVeneer(library *config.Library, googleapisDir string) error {
8888
}
8989
switch sidekickConfig.General.Language {
9090
case "rust":
91-
err = sidekickrust.Generate(model, module.Output, sidekickConfig)
91+
err = sidekickrust.Generate(ctx, model, module.Output, sidekickConfig)
9292
case "rust+prost":
93-
err = rust_prost.Generate(model, module.Output, sidekickConfig)
93+
err = rust_prost.Generate(ctx, model, module.Output, sidekickConfig)
9494
default:
9595
err = fmt.Errorf("unknown language: %s", sidekickConfig.General.Language)
9696
}

internal/librarian/internal/rust/generate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func TestGenerate(t *testing.T) {
180180
if err := Generate(t.Context(), library, sources); err != nil {
181181
t.Fatal(err)
182182
}
183-
if err := Format(library); err != nil {
183+
if err := Format(t.Context(), library); err != nil {
184184
t.Fatal(err)
185185
}
186186

internal/sidekick/codec_sample/generate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package codec_sample
1717

1818
import (
19+
"context"
1920
"embed"
2021

2122
"github.com/googleapis/librarian/internal/sidekick/api"
@@ -27,7 +28,7 @@ import (
2728
var templates embed.FS
2829

2930
// Generate generates code from the model.
30-
func Generate(model *api.API, outdir string, cfg *config.Config) error {
31+
func Generate(ctx context.Context, model *api.API, outdir string, cfg *config.Config) error {
3132
// A template provide converts a template name into the contents.
3233
provider := func(name string) (string, error) {
3334
contents, err := templates.ReadFile(name)

internal/sidekick/codec_sample/generate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestFromProtobuf(t *testing.T) {
5757
if err != nil {
5858
t.Fatal(err)
5959
}
60-
if err := Generate(model, outDir, cfg); err != nil {
60+
if err := Generate(t.Context(), model, outDir, cfg); err != nil {
6161
t.Fatal(err)
6262
}
6363
filename := path.Join(outDir, "README.md")

internal/sidekick/dart/dart.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package dart
1717

1818
import (
19+
"context"
1920
"fmt"
2021
"regexp"
2122
"strings"
@@ -256,8 +257,8 @@ func shouldGenerateMethod(m *api.Method) bool {
256257
return m.PathInfo.Bindings[0].PathTemplate != nil
257258
}
258259

259-
func formatDirectory(dir string) error {
260-
if err := command.Run("dart", "format", dir); err != nil {
260+
func formatDirectory(ctx context.Context, dir string) error {
261+
if err := command.Run(ctx, "dart", "format", dir); err != nil {
261262
return fmt.Errorf("got an error trying to run `dart format`; perhaps try https://dart.dev/get-dart (%w)", err)
262263
}
263264
return nil

internal/sidekick/dart/generate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package dart
1616

1717
import (
18+
"context"
1819
"embed"
1920
"path/filepath"
2021

@@ -27,7 +28,7 @@ import (
2728
var dartTemplates embed.FS
2829

2930
// Generate generates Dart code from the model.
30-
func Generate(model *api.API, outdir string, config *config.Config) error {
31+
func Generate(ctx context.Context, model *api.API, outdir string, config *config.Config) error {
3132
annotate := newAnnotateModel(model)
3233
if err := annotate.annotateModel(config.Codec); err != nil {
3334
return err
@@ -39,7 +40,7 @@ func Generate(model *api.API, outdir string, config *config.Config) error {
3940
// Check if we're configured to skip formatting.
4041
skipFormat := config.Codec["skip-format"]
4142
if skipFormat != "true" {
42-
err = formatDirectory(outdir)
43+
err = formatDirectory(ctx, outdir)
4344
}
4445
}
4546
return err

internal/sidekick/dart/generate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestFromProtobuf(t *testing.T) {
6565
if err != nil {
6666
t.Fatal(err)
6767
}
68-
if err := Generate(model, outDir, cfg); err != nil {
68+
if err := Generate(t.Context(), model, outDir, cfg); err != nil {
6969
t.Fatal(err)
7070
}
7171
for _, expected := range []string{"pubspec.yaml", "lib/secretmanager.dart", "README.md"} {

0 commit comments

Comments
 (0)