You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-09-16 09:26:52 +02:00
fix: return an err with stdout/err when a command failed (#2363)
* fix: return an err with stdout/err when a command failed Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * chore: fmt Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * test: fixes Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
committed by
GitHub
parent
2a304f5932
commit
1dd2ad1ae6
@@ -1,7 +1,6 @@
|
|||||||
package golang
|
package golang
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
@@ -196,8 +195,7 @@ func run(ctx *context.Context, command, env []string, dir string) error {
|
|||||||
cmd.Dir = dir
|
cmd.Dir = dir
|
||||||
log.Debug("running")
|
log.Debug("running")
|
||||||
if out, err := cmd.CombinedOutput(); err != nil {
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
log.WithError(err).Debug("failed")
|
return fmt.Errorf("%w: %s", err, string(out))
|
||||||
return errors.New(string(out))
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -2,13 +2,16 @@
|
|||||||
package exec
|
package exec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
"github.com/caarlos0/go-shellwords"
|
"github.com/caarlos0/go-shellwords"
|
||||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||||
|
"github.com/goreleaser/goreleaser/internal/gio"
|
||||||
"github.com/goreleaser/goreleaser/internal/logext"
|
"github.com/goreleaser/goreleaser/internal/logext"
|
||||||
"github.com/goreleaser/goreleaser/internal/pipe"
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
||||||
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
||||||
@@ -78,12 +81,14 @@ func executeCommand(c *command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fields := log.Fields{"cmd": c.Args[0]}
|
fields := log.Fields{"cmd": c.Args[0]}
|
||||||
cmd.Stderr = logext.NewWriter(fields, logext.Error)
|
var b bytes.Buffer
|
||||||
cmd.Stdout = logext.NewWriter(fields, logext.Info)
|
w := gio.Safe(&b)
|
||||||
|
cmd.Stderr = io.MultiWriter(logext.NewWriter(fields, logext.Error), w)
|
||||||
|
cmd.Stdout = io.MultiWriter(logext.NewWriter(fields, logext.Info), w)
|
||||||
|
|
||||||
log.WithFields(fields).Info("publishing")
|
log.WithFields(fields).Info("publishing")
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return fmt.Errorf("publishing: %s failed: %w", c.Args[0], err)
|
return fmt.Errorf("publishing: %s failed: %w: %s", c.Args[0], err, b.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
log.WithFields(fields).Debugf("command %s finished successfully", c.Args[0])
|
log.WithFields(fields).Debugf("command %s finished successfully", c.Args[0])
|
||||||
|
@@ -251,7 +251,7 @@ func TestExecute(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
// stderr is sent to output via logger
|
// stderr is sent to output via logger
|
||||||
fmt.Errorf(`publishing: %s failed: exit status 1`, MockCmd),
|
fmt.Errorf(`publishing: %s failed: exit status 1: test error`, MockCmd),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -3,6 +3,7 @@ package docker
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -43,7 +44,7 @@ type manifester interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// nolint: unparam
|
// nolint: unparam
|
||||||
func runCommand(ctx context.Context, dir, binary string, args ...string) ([]byte, error) {
|
func runCommand(ctx context.Context, dir, binary string, args ...string) error {
|
||||||
fields := log.Fields{
|
fields := log.Fields{
|
||||||
"cmd": append([]string{binary}, args[0]),
|
"cmd": append([]string{binary}, args[0]),
|
||||||
"cwd": dir,
|
"cwd": dir,
|
||||||
@@ -59,6 +60,8 @@ func runCommand(ctx context.Context, dir, binary string, args ...string) ([]byte
|
|||||||
cmd.Stdout = io.MultiWriter(logext.NewWriter(fields, logext.Info), w)
|
cmd.Stdout = io.MultiWriter(logext.NewWriter(fields, logext.Info), w)
|
||||||
|
|
||||||
log.WithFields(fields).Debug("running")
|
log.WithFields(fields).Debug("running")
|
||||||
err := cmd.Run()
|
if err := cmd.Run(); err != nil {
|
||||||
return b.Bytes(), err
|
return fmt.Errorf("%w: %s", err, b.String())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -17,13 +17,13 @@ func init() {
|
|||||||
type dockerManifester struct{}
|
type dockerManifester struct{}
|
||||||
|
|
||||||
func (m dockerManifester) Create(ctx context.Context, manifest string, images, flags []string) error {
|
func (m dockerManifester) Create(ctx context.Context, manifest string, images, flags []string) error {
|
||||||
_, _ = runCommand(ctx, ".", "docker", "manifest", "rm", manifest)
|
_ = runCommand(ctx, ".", "docker", "manifest", "rm", manifest)
|
||||||
|
|
||||||
args := []string{"manifest", "create", manifest}
|
args := []string{"manifest", "create", manifest}
|
||||||
args = append(args, images...)
|
args = append(args, images...)
|
||||||
args = append(args, flags...)
|
args = append(args, flags...)
|
||||||
|
|
||||||
if _, err := runCommand(ctx, ".", "docker", args...); err != nil {
|
if err := runCommand(ctx, ".", "docker", args...); err != nil {
|
||||||
return fmt.Errorf("failed to create %s: %w", manifest, err)
|
return fmt.Errorf("failed to create %s: %w", manifest, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -32,7 +32,7 @@ func (m dockerManifester) Create(ctx context.Context, manifest string, images, f
|
|||||||
func (m dockerManifester) Push(ctx context.Context, manifest string, flags []string) error {
|
func (m dockerManifester) Push(ctx context.Context, manifest string, flags []string) error {
|
||||||
args := []string{"manifest", "push", manifest}
|
args := []string{"manifest", "push", manifest}
|
||||||
args = append(args, flags...)
|
args = append(args, flags...)
|
||||||
if _, err := runCommand(ctx, ".", "docker", args...); err != nil {
|
if err := runCommand(ctx, ".", "docker", args...); err != nil {
|
||||||
return fmt.Errorf("failed to push %s: %w", manifest, err)
|
return fmt.Errorf("failed to push %s: %w", manifest, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -43,14 +43,14 @@ type dockerImager struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i dockerImager) Push(ctx context.Context, image string, flags []string) error {
|
func (i dockerImager) Push(ctx context.Context, image string, flags []string) error {
|
||||||
if _, err := runCommand(ctx, ".", "docker", "push", image); err != nil {
|
if err := runCommand(ctx, ".", "docker", "push", image); err != nil {
|
||||||
return fmt.Errorf("failed to push %s: %w", image, err)
|
return fmt.Errorf("failed to push %s: %w", image, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i dockerImager) Build(ctx context.Context, root string, images, flags []string) error {
|
func (i dockerImager) Build(ctx context.Context, root string, images, flags []string) error {
|
||||||
if _, err := runCommand(ctx, root, "docker", i.buildCommand(images, flags)...); err != nil {
|
if err := runCommand(ctx, root, "docker", i.buildCommand(images, flags)...); err != nil {
|
||||||
return fmt.Errorf("failed to build %s: %w", images[0], err)
|
return fmt.Errorf("failed to build %s: %w", images[0], err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package sign
|
package sign
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||||
|
"github.com/goreleaser/goreleaser/internal/gio"
|
||||||
"github.com/goreleaser/goreleaser/internal/ids"
|
"github.com/goreleaser/goreleaser/internal/ids"
|
||||||
"github.com/goreleaser/goreleaser/internal/logext"
|
"github.com/goreleaser/goreleaser/internal/logext"
|
||||||
"github.com/goreleaser/goreleaser/internal/pipe"
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
||||||
@@ -153,14 +155,16 @@ func signone(ctx *context.Context, cfg config.Sign, a *artifact.Artifact) (*arti
|
|||||||
// tells the scanner to ignore this.
|
// tells the scanner to ignore this.
|
||||||
// #nosec
|
// #nosec
|
||||||
cmd := exec.CommandContext(ctx, cfg.Cmd, args...)
|
cmd := exec.CommandContext(ctx, cfg.Cmd, args...)
|
||||||
cmd.Stderr = logext.NewWriter(fields, logext.Error)
|
var b bytes.Buffer
|
||||||
cmd.Stdout = logext.NewWriter(fields, logext.Info)
|
w := gio.Safe(&b)
|
||||||
|
cmd.Stderr = io.MultiWriter(logext.NewWriter(fields, logext.Error), w)
|
||||||
|
cmd.Stdout = io.MultiWriter(logext.NewWriter(fields, logext.Info), w)
|
||||||
if stdin != nil {
|
if stdin != nil {
|
||||||
cmd.Stdin = stdin
|
cmd.Stdin = stdin
|
||||||
}
|
}
|
||||||
log.WithFields(fields).Info("signing")
|
log.WithFields(fields).Info("signing")
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return nil, fmt.Errorf("sign: %s failed", cfg.Cmd)
|
return nil, fmt.Errorf("sign: %s failed: %w: %s", cfg.Cmd, err, b.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
artifactPathBase, _ := filepath.Split(a.Path)
|
artifactPathBase, _ := filepath.Split(a.Path)
|
||||||
|
@@ -327,7 +327,7 @@ func create(ctx *context.Context, snap config.Snapcraft, arch string, binaries [
|
|||||||
/* #nosec */
|
/* #nosec */
|
||||||
cmd := exec.CommandContext(ctx, "snapcraft", "pack", primeDir, "--output", snapFile)
|
cmd := exec.CommandContext(ctx, "snapcraft", "pack", primeDir, "--output", snapFile)
|
||||||
if out, err = cmd.CombinedOutput(); err != nil {
|
if out, err = cmd.CombinedOutput(); err != nil {
|
||||||
return fmt.Errorf("failed to generate snap package: %s", string(out))
|
return fmt.Errorf("failed to generate snap package: %w: %s", err, string(out))
|
||||||
}
|
}
|
||||||
if !snap.Publish {
|
if !snap.Publish {
|
||||||
return nil
|
return nil
|
||||||
@@ -362,7 +362,7 @@ func push(ctx *context.Context, snap *artifact.Artifact) error {
|
|||||||
if strings.Contains(string(out), reviewWaitMsg) || strings.Contains(string(out), humanReviewMsg) || strings.Contains(string(out), needsReviewMsg) {
|
if strings.Contains(string(out), reviewWaitMsg) || strings.Contains(string(out), humanReviewMsg) || strings.Contains(string(out), needsReviewMsg) {
|
||||||
log.Warn(reviewWaitMsg)
|
log.Warn(reviewWaitMsg)
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("failed to push %s package: %s", snap.Path, string(out))
|
return fmt.Errorf("failed to push %s package: %w: %s", snap.Path, err, string(out))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
snap.Type = artifact.Snapcraft
|
snap.Type = artifact.Snapcraft
|
||||||
|
Reference in New Issue
Block a user