Skip to content

Commit 346bec2

Browse files
Enable running scripts in parallel (#139)
* Make it possible to run multiple scripts in parallel * Code review changes * Fix references after rebase on main
1 parent 9d3c523 commit 346bec2

File tree

7 files changed

+342
-219
lines changed

7 files changed

+342
-219
lines changed

cmd/run.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
package cmd
22

33
import (
4+
"errors"
5+
46
log "github.com/sirupsen/logrus"
57
"github.com/spf13/cobra"
8+
"golang.org/x/sync/errgroup"
69
)
710

811
// runCmd represents the version command
912
var runCmd = &cobra.Command{
10-
Use: "run <script>",
11-
Short: "Executes a script",
12-
Args: cobra.ExactArgs(1),
13+
Use: "run [scripts]",
14+
Short: "Executes one or more scripts in parallel",
15+
Long: `Executes one or more scripts in parallel
16+
All scripts will run to completion, regardless of whether or not the other scripts exit with errors.
17+
18+
Should any of the scripts fail Leeway will exit with an exit code of 1 once all scripts are done executing.
19+
`,
20+
Args: cobra.MinimumNArgs(1),
1321
Run: func(cmd *cobra.Command, args []string) {
14-
_, _, script, _ := getTarget(args, true)
15-
if script == nil {
16-
log.Fatal("tree needs a package")
22+
g := new(errgroup.Group)
23+
for _, scriptName := range args {
24+
scriptName := scriptName
25+
g.Go(func() error {
26+
_, _, script, _ := getTarget([]string{scriptName}, true)
27+
if script == nil {
28+
return errors.New("run needs a script")
29+
}
30+
opts, _ := getBuildOpts(cmd)
31+
return script.Run(opts...)
32+
})
1733
}
18-
19-
opts, _ := getBuildOpts(cmd)
20-
err := script.Run(opts...)
34+
err := g.Wait()
2135
if err != nil {
2236
log.Fatal(err)
2337
}

pkg/leeway/build_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fi
3636
`
3737

3838
func TestBuildDockerDeps(t *testing.T) {
39-
if *dut {
39+
if *testutil.Dut {
4040
pth, err := os.MkdirTemp("", "")
4141
if err != nil {
4242
t.Fatal(err)
@@ -50,9 +50,9 @@ func TestBuildDockerDeps(t *testing.T) {
5050
os.Setenv("PATH", pth+":"+os.Getenv("PATH"))
5151
log.WithField("path", os.Getenv("PATH")).Debug("modified path to use dummy docker")
5252
}
53-
runDUT()
53+
testutil.RunDUT()
5454

55-
tests := []*CommandFixtureTest{
55+
tests := []*testutil.CommandFixtureTest{
5656
{
5757
Name: "docker dependency",
5858
T: t,

pkg/leeway/fixtures/scripts.yaml

Lines changed: 0 additions & 38 deletions
This file was deleted.

pkg/leeway/scripts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func executeBashScript(script string, wd string, env []string) error {
253253
err = cmd.Run()
254254
if exiterr, ok := err.(*exec.ExitError); ok {
255255
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
256-
os.Exit(status.ExitStatus())
256+
return xerrors.Errorf("failed with exit code %d", status.ExitStatus())
257257
}
258258
}
259259
return err

0 commit comments

Comments
 (0)