2020-05-26 05:48:10 +02:00
|
|
|
// Package before provides the pipe implementation that runs before all other pipes.
|
2018-03-28 15:31:09 +02:00
|
|
|
package before
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-03-22 13:45:18 +02:00
|
|
|
"github.com/caarlos0/go-shellwords"
|
2022-06-22 02:11:15 +02:00
|
|
|
"github.com/caarlos0/log"
|
2023-11-30 03:01:13 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/shell"
|
2023-09-16 22:01:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/skips"
|
2019-03-03 19:12:22 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2018-03-28 15:31:09 +02:00
|
|
|
)
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Pipe is a global hook pipe.
|
2018-03-28 15:31:09 +02:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2022-06-23 02:56:53 +02:00
|
|
|
func (Pipe) String() string { return "running before hooks" }
|
2023-09-16 22:01:20 +02:00
|
|
|
|
2022-06-23 02:56:53 +02:00
|
|
|
func (Pipe) Skip(ctx *context.Context) bool {
|
2023-09-16 22:01:20 +02:00
|
|
|
return len(ctx.Config.Before.Hooks) == 0 || skips.Any(ctx, skips.Before)
|
2022-06-23 02:56:53 +02:00
|
|
|
}
|
2018-03-28 15:31:09 +02:00
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Run executes the hooks.
|
2018-03-28 15:31:09 +02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2021-03-22 13:45:18 +02:00
|
|
|
tmpl := tmpl.New(ctx)
|
2018-03-28 15:31:09 +02:00
|
|
|
/* #nosec */
|
|
|
|
for _, step := range ctx.Config.Before.Hooks {
|
2019-03-03 19:12:22 +02:00
|
|
|
s, err := tmpl.Apply(step)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-12 19:12:53 +02:00
|
|
|
args, err := shellwords.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-23 13:09:29 +02:00
|
|
|
|
2023-11-30 03:01:13 +02:00
|
|
|
log.WithField("hook", s).Info("running")
|
|
|
|
if err := shell.Run(ctx, "", args, ctx.Env.Strings(), false); err != nil {
|
|
|
|
return fmt.Errorf("hook failed: %w", err)
|
2018-03-28 15:31:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|