github.com/amane3/goreleaser@v0.182.0/internal/pipe/before/before.go (about)

     1  // Package before provides the pipe implementation that runs before all other pipes.
     2  package before
     3  
     4  import (
     5  	"fmt"
     6  	"os/exec"
     7  
     8  	"github.com/amane3/goreleaser/internal/tmpl"
     9  	"github.com/amane3/goreleaser/pkg/context"
    10  	"github.com/apex/log"
    11  	"github.com/fatih/color"
    12  	"github.com/mattn/go-shellwords"
    13  )
    14  
    15  // Pipe is a global hook pipe.
    16  type Pipe struct{}
    17  
    18  // String is the name of this pipe.
    19  func (Pipe) String() string {
    20  	return "running before hooks"
    21  }
    22  
    23  // Run executes the hooks.
    24  func (Pipe) Run(ctx *context.Context) error {
    25  	var tmpl = tmpl.New(ctx)
    26  	/* #nosec */
    27  	for _, step := range ctx.Config.Before.Hooks {
    28  		s, err := tmpl.Apply(step)
    29  		if err != nil {
    30  			return err
    31  		}
    32  		args, err := shellwords.Parse(s)
    33  		if err != nil {
    34  			return err
    35  		}
    36  		log.Infof("running %s", color.CyanString(step))
    37  		cmd := exec.Command(args[0], args[1:]...)
    38  		cmd.Env = ctx.Env.Strings()
    39  		out, err := cmd.CombinedOutput()
    40  		log.WithField("cmd", step).Debug(string(out))
    41  		if err != nil {
    42  			return fmt.Errorf("hook failed: %s: %w; output: %s", step, err, string(out))
    43  		}
    44  	}
    45  	return nil
    46  }