1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-19 20:57:53 +02:00
Oleksandr Redko bae3bacc7d
refactor: use cmp.Or instead of ordered.First (#5295)
This PR replaces usages of
[`ordered.First`](https://pkg.go.dev/github.com/charmbracelet/x/exp/ordered#First)
with [`cmp.Or`](https://pkg.go.dev/cmp#Or). No need to use an external
library when the same functionality is present in stdlib.

You can compare implementations:
[ordered.First](https://github.com/charmbracelet/x/blob/exp/ordered/v0.1.0/exp/ordered/ordered.go#L31)
vs
[cmp.Or](https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/cmp/cmp.go;l=69).

Additional reading
https://blog.carlana.net/post/2024/golang-cmp-or-uses-and-history/
2024-11-18 14:05:41 -03:00

49 lines
999 B
Go

package shell
import (
"bytes"
"cmp"
"fmt"
"io"
"os/exec"
"strings"
"github.com/caarlos0/log"
"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,
cmp.Or(strings.TrimSpace(b.String()), "[no output]"),
)
}
return nil
}