Skip to content

Commit

Permalink
fix: exit status should be 1 on build error (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
kperreau authored Jul 1, 2024
1 parent b44f0f7 commit f794cc4
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .semver.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
alpha: 0
beta: 0
rc: 0
release: v1.1.0
release: v1.1.1
5 changes: 4 additions & 1 deletion cmd/affected.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package cmd
import (
"errors"
"fmt"
"os"
"slices"
"strings"

"github.com/kperreau/goac/pkg/printer"
"github.com/kperreau/goac/pkg/project"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -39,7 +41,8 @@ var affectedCmd = &cobra.Command{
return err
}
if err := projectsList.Affected(); err != nil {
return err
printer.Errorf("%s\n", err)
os.Exit(1)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package cmd
import (
"errors"
"fmt"
"gopkg.in/yaml.v3"
"os"

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

// versionCmd represents the project command
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ go 1.22

require (
github.com/codeskyblue/dockerignore v0.0.0-20151214070507-de82dee623d9
github.com/fatih/color v1.16.0
github.com/spf13/cobra v1.8.0
github.com/fatih/color v1.17.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
golang.org/x/mod v0.17.0
golang.org/x/mod v0.18.0
golang.org/x/sync v0.7.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -19,5 +20,5 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/smartystreets/goconvey v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/sys v0.21.0 // indirect
)
20 changes: 11 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
github.com/codeskyblue/dockerignore v0.0.0-20151214070507-de82dee623d9 h1:c9axcChJwkLuSl9AvwTHi8jiBa6+VX4gGgERhABgv2E=
github.com/codeskyblue/dockerignore v0.0.0-20151214070507-de82dee623d9/go.mod h1:XNZkUhPf+qgRnhY/ecS3B73ODJ2NXCzDMJHXM069IMg=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand All @@ -23,18 +23,20 @@ github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGB
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
45 changes: 16 additions & 29 deletions pkg/project/affected.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package project

import (
"fmt"
"path"
"sync"

"github.com/fatih/color"
"github.com/kperreau/goac/pkg/printer"
"github.com/kperreau/goac/pkg/utils"
"golang.org/x/sync/errgroup"
)

type Target string
Expand All @@ -19,58 +20,44 @@ const (

func (t Target) String() string { return string(t) }

type processAffectedOptions struct {
wg *sync.WaitGroup
sem chan bool
}

func (l *List) Affected() error {
l.printAffected()

// init process options
sem := make(chan bool, l.Options.MaxConcurrency)
wg := sync.WaitGroup{}
pOpts := &processAffectedOptions{
wg: &wg,
sem: sem,
}

eg := errgroup.Group{}
eg.SetLimit(l.Options.MaxConcurrency)
for _, p := range l.Projects {
sem <- true // acquire
wg.Add(1)
go processAffected(p, pOpts)
eg.Go(func() error {
return processAffected(p)
})
}

wg.Wait()
if err := eg.Wait(); err != nil {
return err
}

return nil
}

func processAffected(p *Project, opts *processAffectedOptions) {
defer opts.wg.Done()
defer func() {
<-opts.sem // release
}()

func processAffected(p *Project) error {
isAffected := p.isAffected()

if isAffected && p.CMDOptions.DryRun {
printer.Printf("%s %s %s\n", color.BlueString(p.Name), color.YellowString("=>"), p.CleanPath)
}

if p.CMDOptions.DryRun || !isAffected {
return
return nil
}

if err := p.build(); err != nil {
printer.Errorf("error building: %s\n", err.Error())
return
return fmt.Errorf("error building: %s", err.Error())
}

if err := p.writeCache(); err != nil {
printer.Errorf("%v\n", err)
return
return err
}

return nil
}

func (l *List) countAffected() (n int) {
Expand Down
16 changes: 2 additions & 14 deletions pkg/project/affected_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"log"
"os"
"sync"
"testing"

"github.com/fatih/color"
Expand Down Expand Up @@ -463,25 +462,14 @@ func TestProcessAffected_BuildAffectedProject(t *testing.T) {
},
}

// Create a processAffectedOptions with a wait group and semaphore
opts := &processAffectedOptions{
wg: &sync.WaitGroup{},
sem: make(chan bool, 1),
}

// Add a wait group counter
opts.wg.Add(1)

// Acquire the semaphore
opts.sem <- true

// Redirect stdout to a buffer
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// Call the processAffected function
processAffected(p, opts)
err := processAffected(p)
assert.NoError(t, err)

// Restore stdout
if err := w.Close(); err != nil {
Expand Down

0 comments on commit f794cc4

Please sign in to comment.