github.com/goreleaser/goreleaser@v1.25.1/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  
     7  	"github.com/caarlos0/go-shellwords"
     8  	"github.com/caarlos0/log"
     9  	"github.com/goreleaser/goreleaser/internal/shell"
    10  	"github.com/goreleaser/goreleaser/internal/skips"
    11  	"github.com/goreleaser/goreleaser/internal/tmpl"
    12  	"github.com/goreleaser/goreleaser/pkg/context"
    13  )
    14  
    15  // Pipe is a global hook pipe.
    16  type Pipe struct{}
    17  
    18  func (Pipe) String() string { return "running before hooks" }
    19  
    20  func (Pipe) Skip(ctx *context.Context) bool {
    21  	return len(ctx.Config.Before.Hooks) == 0 || skips.Any(ctx, skips.Before)
    22  }
    23  
    24  // Run executes the hooks.
    25  func (Pipe) Run(ctx *context.Context) error {
    26  	tmpl := tmpl.New(ctx)
    27  	/* #nosec */
    28  	for _, step := range ctx.Config.Before.Hooks {
    29  		s, err := tmpl.Apply(step)
    30  		if err != nil {
    31  			return err
    32  		}
    33  		args, err := shellwords.Parse(s)
    34  		if err != nil {
    35  			return err
    36  		}
    37  
    38  		log.WithField("hook", s).Info("running")
    39  		if err := shell.Run(ctx, "", args, ctx.Env.Strings(), false); err != nil {
    40  			return fmt.Errorf("hook failed: %w", err)
    41  		}
    42  	}
    43  	return nil
    44  }