diff --git a/README.md b/README.md index 8a420794..f06d1789 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # magetools -![Coverage](https://img.shields.io/badge/Coverage-54.2%25-yellow) +![Coverage](https://img.shields.io/badge/Coverage-53.9%25-yellow) General tooling helpers for simplifying cross repository automation using Mage. diff --git a/tooling/tooling.go b/tooling/tooling.go index ac005920..4e152db7 100644 --- a/tooling/tooling.go +++ b/tooling/tooling.go @@ -60,6 +60,7 @@ func InstallTools(tools []string) error { // Originally found from: https://www.yellowduck.be/posts/reading-command-output-line-by-line/ and modified. //nolint:funlen // This is ok for now. Can refactor into smaller pieces later if needed. func SilentInstallTools(toolList []string) error { + var errorCount int magetoolsutils.CheckPtermDebug() if ci.IsCI() { pterm.DisableStyling() @@ -76,11 +77,7 @@ func SilentInstallTools(toolList []string) error { // WithSequence("|", "/", "-", "|", "/", "-", "\\"). // spinnerLiveText, _ := pterm.DefaultSpinner.Start("InstallTools") - defer func() { - msg := fmt.Sprintf("SilentInstallTools: %s\n", humanize.Time(start)) - spin.Success(msg) // Resolve spinner with success message. - // pterm.Success.Println(msg) - }() + pterm.Info.Printf("items to iterate through: %d", len(toolList)) for _, item := range toolList { cmd := exec.Command("go", "install", item) @@ -114,9 +111,12 @@ func SilentInstallTools(toolList []string) error { // Start the command and check for errors err := cmd.Start() if err != nil { + pterm.Error.Printfln("unable to install: %q %v", item, err) + errorCount++ spin.Fail(err) - _ = spin.Stop() - return err + continue + // _ = spin.Stop() // NOTE: continue installing other tools, don't stop everything, just fail this and count it + // return err } // Wait for all output to be processed @@ -126,11 +126,21 @@ func SilentInstallTools(toolList []string) error { err = cmd.Wait() if err != nil { spin.Fail(err) - _ = spin.Stop() - return err + errorCount++ + pterm.Error.Printfln("unable to install: %q %v", item, err) + // _ = spin.Stop() // NOTE: continue installing other tools, don't stop everything, just fail this and count it + // return err + continue } spin.Success(item) } + + if errorCount > 0 { + pterm.Error.Printfln("SilentInstallTools: total errors: [%d] %s\n", errorCount, humanize.Time(start)) + return fmt.Errorf("SilentInstallTools: total errors: [%d]", errorCount) + } + msg := fmt.Sprintf("SilentInstallTools: %s\n", humanize.Time(start)) + pterm.Success.Println(msg) return nil } diff --git a/tooling/tooling_test.go b/tooling/tooling_test.go index 88382490..f04c116f 100644 --- a/tooling/tooling_test.go +++ b/tooling/tooling_test.go @@ -37,28 +37,47 @@ func TestInstallTools(t *testing.T) { // Try cleaning a specific directory to see output if already ran // sudo rm -rf $(go env GOPATH)/pkg/mod/github.com/fatih. func TestGo_SilentInit(t *testing.T) { - if !strings.Contains(strings.ToLower(os.Getenv("GOTESTS")), "slow") { - t.Skip("GOTESTS should include 'slow' to run this test") - } - is := iz.New(t) - if os.Getenv("PRESENTATION_TEST") != "1" { - t.Skip("PRESENTATION_TEST != 1 so skipping") - } - // pterm.DisableColor() - // pterm.DisableStyling() - toolList := []string{ - "github.com/goreleaser/goreleaser@v0.174.1", - "github.com/golangci/golangci-lint/cmd/golangci-lint@master", - "github.com/dustinkirkland/golang-petname/cmd/petname@master", - "mvdan.cc/gofumpt@latest", - "golang.org/x/tools/gopls@latest", - "github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest", - "github.com/ramya-rao-a/go-outline@latest", - "github.com/cweill/gotests/gotests@latest", - "github.com/fatih/gomodifytags@latest", - } - err := tooling.SilentInstallTools(toolList) - is.NoErr(err) // Init should not fail + t.Run("install list of tools", func(t *testing.T) { + if !strings.Contains(strings.ToLower(os.Getenv("GOTESTS")), "slow") { + t.Skip("GOTESTS should include 'slow' to run this test") + } + is := iz.New(t) + if os.Getenv("PRESENTATION_TEST") != "1" { + t.Skip("PRESENTATION_TEST != 1 so skipping") + } + // pterm.DisableColor() + // pterm.DisableStyling() + toolList := []string{ + "github.com/goreleaser/goreleaser@v0.174.1", + "github.com/golangci/golangci-lint/cmd/golangci-lint@master", + "github.com/dustinkirkland/golang-petname/cmd/petname@master", + "mvdan.cc/gofumpt@latest", + "golang.org/x/tools/gopls@latest", + "github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest", + "github.com/ramya-rao-a/go-outline@latest", + "github.com/cweill/gotests/gotests@latest", + "github.com/fatih/gomodifytags@latest", + } + err := tooling.SilentInstallTools(toolList) + is.NoErr(err) // Init should not fail + }) + + t.Run("fail on item 2 and advise what failed", func(t *testing.T) { + if !strings.Contains(strings.ToLower(os.Getenv("GOTESTS")), "slow") { + t.Skip("GOTESTS should include 'slow' to run this test") + } + is := iz.New(t) + if os.Getenv("PRESENTATION_TEST") != "1" { + t.Skip("PRESENTATION_TEST != 1 so skipping") + } + toolList := []string{ + "github.com/goreleaser/goreleaser@v0.174.1", + "github.com/golangci/golangci-lint/cmd/golangci-lintINVALID@master", + "github.com/dustinkirkland/golang-petname/cmd/petname@master", + } + err := tooling.SilentInstallTools(toolList) + is.True(err != nil) // An error should be returned with useful context should not fail + }) } // This is a test for the minimal output, doesn't need to run unless manully invoked with: