1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-22 04:08:49 +02:00

56 lines
1.4 KiB
Go
Raw Normal View History

// Package before provides the pipe implementation that runs before all other pipes.
2018-03-28 15:31:09 +02:00
package before
import (
"bytes"
2018-03-28 15:31:09 +02:00
"fmt"
"io"
2018-03-28 15:31:09 +02:00
"os/exec"
"github.com/caarlos0/go-shellwords"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/gio"
"github.com/goreleaser/goreleaser/internal/logext"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/context"
2018-03-28 15:31:09 +02:00
)
// Pipe is a global hook pipe.
2018-03-28 15:31:09 +02:00
type Pipe struct{}
func (Pipe) String() string { return "running before hooks" }
func (Pipe) Skip(ctx *context.Context) bool {
return len(ctx.Config.Before.Hooks) == 0 || ctx.SkipBefore
}
2018-03-28 15:31:09 +02:00
// Run executes the hooks.
2018-03-28 15:31:09 +02:00
func (Pipe) Run(ctx *context.Context) error {
tmpl := tmpl.New(ctx)
2018-03-28 15:31:09 +02:00
/* #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
}
2018-03-28 15:31:09 +02:00
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = ctx.Env.Strings()
var b bytes.Buffer
w := gio.Safe(&b)
fields := log.Fields{"hook": step}
cmd.Stderr = io.MultiWriter(logext.NewWriter(fields, logext.Error), w)
cmd.Stdout = io.MultiWriter(logext.NewWriter(fields, logext.Info), w)
log.WithFields(fields).Info("running")
if err := cmd.Run(); err != nil {
return fmt.Errorf("hook failed: %s: %w; output: %s", step, err, b.String())
2018-03-28 15:31:09 +02:00
}
}
return nil
}