github.com/triarius/goreleaser@v1.12.5/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/triarius/goreleaser/internal/gio"
    12  	"github.com/triarius/goreleaser/internal/logext"
    13  	"github.com/triarius/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  	fields := log.Fields{
    19  		"cmd": command,
    20  		"env": env,
    21  	}
    22  
    23  	/* #nosec */
    24  	cmd := exec.CommandContext(ctx, command[0], command[1:]...)
    25  	cmd.Env = env
    26  
    27  	var b bytes.Buffer
    28  	w := gio.Safe(&b)
    29  
    30  	cmd.Stderr = io.MultiWriter(logext.NewConditionalWriter(fields, logext.Error, output), w)
    31  	cmd.Stdout = io.MultiWriter(logext.NewConditionalWriter(fields, logext.Info, output), w)
    32  
    33  	if dir != "" {
    34  		cmd.Dir = dir
    35  	}
    36  
    37  	log.WithFields(fields).Debug("running")
    38  	if err := cmd.Run(); err != nil {
    39  		log.WithFields(fields).WithError(err).Debug("failed")
    40  		return fmt.Errorf("failed to run '%s': %w", strings.Join(command, " "), err)
    41  	}
    42  
    43  	return nil
    44  }