2021-11-23 15:53:12 -03:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
2022-02-05 16:00:49 -03:00
|
|
|
"strings"
|
2021-11-23 15:53:12 -03:00
|
|
|
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2021-11-23 15:53:12 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/gio"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/logext"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run a shell command with given arguments and envs
|
2022-02-05 16:00:49 -03:00
|
|
|
func Run(ctx *context.Context, dir string, command, env []string, output bool) error {
|
2023-05-02 09:06:35 -03:00
|
|
|
log := log.
|
|
|
|
WithField("cmd", command).
|
|
|
|
WithField("env", env)
|
2021-11-23 15:53:12 -03:00
|
|
|
|
|
|
|
/* #nosec */
|
|
|
|
cmd := exec.CommandContext(ctx, command[0], command[1:]...)
|
|
|
|
cmd.Env = env
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
w := gio.Safe(&b)
|
|
|
|
|
2023-02-01 23:25:36 -03:00
|
|
|
cmd.Stderr = io.MultiWriter(logext.NewConditionalWriter(output), w)
|
|
|
|
cmd.Stdout = io.MultiWriter(logext.NewConditionalWriter(output), w)
|
2021-11-23 15:53:12 -03:00
|
|
|
|
|
|
|
if dir != "" {
|
|
|
|
cmd.Dir = dir
|
|
|
|
}
|
|
|
|
|
2023-05-02 09:06:35 -03:00
|
|
|
log.Debug("running")
|
2021-11-23 15:53:12 -03:00
|
|
|
if err := cmd.Run(); err != nil {
|
2023-05-02 09:06:35 -03:00
|
|
|
log.WithError(err).Debug("failed")
|
2022-02-05 16:00:49 -03:00
|
|
|
return fmt.Errorf("failed to run '%s': %w", strings.Join(command, " "), err)
|
2021-11-23 15:53:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|