2020-05-26 00:48:10 -03: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"
|
|
|
|
"os/exec"
|
|
|
|
|
2018-04-03 21:35:48 -03:00
|
|
|
"github.com/apex/log"
|
2021-03-22 08:45:18 -03:00
|
|
|
"github.com/caarlos0/go-shellwords"
|
2018-04-03 21:35:48 -03:00
|
|
|
"github.com/fatih/color"
|
2019-03-03 14:12:22 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
2018-08-14 23:50:20 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2018-03-28 15:31:09 +02:00
|
|
|
)
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Pipe is a global hook pipe.
|
2018-03-28 15:31:09 +02:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// String is the name of this pipe.
|
2018-03-28 15:31:09 +02:00
|
|
|
func (Pipe) String() string {
|
2020-03-06 01:25:09 -03:00
|
|
|
return "running before hooks"
|
2018-03-28 15:31:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Run executes the hooks.
|
2018-03-28 15:31:09 +02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2021-03-22 08:45:18 -03:00
|
|
|
tmpl := tmpl.New(ctx)
|
2018-03-28 15:31:09 +02:00
|
|
|
/* #nosec */
|
|
|
|
for _, step := range ctx.Config.Before.Hooks {
|
2019-03-03 14:12:22 -03:00
|
|
|
s, err := tmpl.Apply(step)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-12 14:12:53 -03:00
|
|
|
args, err := shellwords.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-03 21:35:48 -03:00
|
|
|
log.Infof("running %s", color.CyanString(step))
|
2018-03-28 15:31:09 +02:00
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
2019-04-09 09:15:05 -03:00
|
|
|
cmd.Env = ctx.Env.Strings()
|
2018-04-10 14:35:45 -03:00
|
|
|
out, err := cmd.CombinedOutput()
|
2020-07-22 09:14:56 -03:00
|
|
|
log.WithField("cmd", step).Debug(string(out))
|
2018-04-10 14:35:45 -03:00
|
|
|
if err != nil {
|
2020-11-10 11:15:03 -03:00
|
|
|
return fmt.Errorf("hook failed: %s: %w; output: %s", step, err, string(out))
|
2018-03-28 15:31:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|