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