1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-05 13:15:26 +02:00
Carlos Alexandro Becker ec2db4a727
feat!: rename module to /v2 (#4894)
<!--

Hi, thanks for contributing!

Please make sure you read our CONTRIBUTING guide.

Also, add tests and the respective documentation changes as well.

-->


<!-- If applied, this commit will... -->

...

<!-- Why is this change being made? -->

...

<!-- # Provide links to any relevant tickets, URLs or other resources
-->

...

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2024-05-26 15:02:57 -03:00

52 lines
1.0 KiB
Go

package shell
import (
"bytes"
"fmt"
"io"
"os/exec"
"strings"
"github.com/caarlos0/log"
"github.com/charmbracelet/x/exp/ordered"
"github.com/goreleaser/goreleaser/v2/internal/gio"
"github.com/goreleaser/goreleaser/v2/internal/logext"
"github.com/goreleaser/goreleaser/v2/pkg/context"
)
// Run a shell command with given arguments and envs
func Run(ctx *context.Context, dir string, command, env []string, output bool) error {
log := log.
WithField("cmd", command).
WithField("dir", dir)
/* #nosec */
cmd := exec.CommandContext(ctx, command[0], command[1:]...)
cmd.Env = env
var b bytes.Buffer
w := gio.Safe(&b)
cmd.Stderr = io.MultiWriter(logext.NewConditionalWriter(output), w)
cmd.Stdout = io.MultiWriter(logext.NewConditionalWriter(output), w)
if dir != "" {
cmd.Dir = dir
}
log.Debug("running")
if err := cmd.Run(); err != nil {
return fmt.Errorf(
"shell: '%s': %w: %s",
strings.Join(command, " "),
err,
ordered.First(
strings.TrimSpace(b.String()),
"[no output]",
),
)
}
return nil
}