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"
|
2023-11-29 22:01:13 -03:00
|
|
|
"github.com/charmbracelet/x/exp/ordered"
|
2024-05-26 15:02:57 -03:00
|
|
|
"github.com/goreleaser/goreleaser/v2/internal/gio"
|
|
|
|
"github.com/goreleaser/goreleaser/v2/internal/logext"
|
|
|
|
"github.com/goreleaser/goreleaser/v2/pkg/context"
|
2021-11-23 15:53:12 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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).
|
2023-11-29 22:01:13 -03:00
|
|
|
WithField("dir", dir)
|
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-11-29 22:01:13 -03:00
|
|
|
return fmt.Errorf(
|
|
|
|
"shell: '%s': %w: %s",
|
|
|
|
strings.Join(command, " "),
|
|
|
|
err,
|
|
|
|
ordered.First(
|
|
|
|
strings.TrimSpace(b.String()),
|
|
|
|
"[no output]",
|
|
|
|
),
|
|
|
|
)
|
2021-11-23 15:53:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|