github.com/windmeup/goreleaser@v1.21.95/internal/shell/shell.go (about)

     1  package shell
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"os/exec"
     8  	"strings"
     9  
    10  	"github.com/caarlos0/log"
    11  	"github.com/windmeup/goreleaser/internal/gio"
    12  	"github.com/windmeup/goreleaser/internal/logext"
    13  	"github.com/windmeup/goreleaser/pkg/context"
    14  )
    15  
    16  // Run a shell command with given arguments and envs
    17  func Run(ctx *context.Context, dir string, command, env []string, output bool) error {
    18  	log := log.
    19  		WithField("cmd", command).
    20  		WithField("env", env)
    21  
    22  	/* #nosec */
    23  	cmd := exec.CommandContext(ctx, command[0], command[1:]...)
    24  	cmd.Env = env
    25  
    26  	var b bytes.Buffer
    27  	w := gio.Safe(&b)
    28  
    29  	cmd.Stderr = io.MultiWriter(logext.NewConditionalWriter(output), w)
    30  	cmd.Stdout = io.MultiWriter(logext.NewConditionalWriter(output), w)
    31  
    32  	if dir != "" {
    33  		cmd.Dir = dir
    34  	}
    35  
    36  	log.Debug("running")
    37  	if err := cmd.Run(); err != nil {
    38  		log.WithError(err).Debug("failed")
    39  		return fmt.Errorf("failed to run '%s': %w", strings.Join(command, " "), err)
    40  	}
    41  
    42  	return nil
    43  }