mirror of
https://github.com/goreleaser/goreleaser.git
synced 2024-12-29 01:44:39 +02:00
parent
482cc6479c
commit
f585f3be69
@ -147,7 +147,7 @@ func runHook(ctx *context.Context, opts builders.Options, buildEnv []string, hoo
|
||||
return err
|
||||
}
|
||||
|
||||
if err := shell.Run(ctx, dir, cmd, env); err != nil {
|
||||
if err := shell.Run(ctx, dir, cmd, env, hook.Output); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ func runHook(ctx *context.Context, hooks config.Hooks) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := shell.Run(ctx, dir, cmd, envs); err != nil {
|
||||
if err := shell.Run(ctx, dir, cmd, envs, hook.Output); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/apex/log"
|
||||
|
||||
@ -14,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
// Run a shell command with given arguments and envs
|
||||
func Run(ctx *context.Context, dir string, command, env []string) error {
|
||||
func Run(ctx *context.Context, dir string, command, env []string, output bool) error {
|
||||
fields := log.Fields{
|
||||
"cmd": command,
|
||||
"env": env,
|
||||
@ -27,8 +28,8 @@ func Run(ctx *context.Context, dir string, command, env []string) error {
|
||||
var b bytes.Buffer
|
||||
w := gio.Safe(&b)
|
||||
|
||||
cmd.Stderr = io.MultiWriter(logext.NewWriter(fields, logext.Error), w)
|
||||
cmd.Stdout = io.MultiWriter(logext.NewWriter(fields, logext.Info), w)
|
||||
cmd.Stderr = io.MultiWriter(logext.NewConditionalWriter(fields, logext.Error, output), w)
|
||||
cmd.Stdout = io.MultiWriter(logext.NewConditionalWriter(fields, logext.Info, output), w)
|
||||
|
||||
if dir != "" {
|
||||
cmd.Dir = dir
|
||||
@ -37,7 +38,7 @@ func Run(ctx *context.Context, dir string, command, env []string) error {
|
||||
log.WithFields(fields).Debug("running")
|
||||
if err := cmd.Run(); err != nil {
|
||||
log.WithFields(fields).WithError(err).Debug("failed")
|
||||
return fmt.Errorf("%q: %w", b.String(), err)
|
||||
return fmt.Errorf("failed to run '%s': %w", strings.Join(command, " "), err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -1,31 +1,39 @@
|
||||
package shell_test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/goreleaser/goreleaser/internal/shell"
|
||||
"github.com/goreleaser/goreleaser/pkg/config"
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRunAValidCommand(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
func TestRunCommand(t *testing.T) {
|
||||
t.Run("simple", func(t *testing.T) {
|
||||
require.NoError(t, shell.Run(context.New(config.Project{}), "", []string{"echo", "oi"}, []string{}, false))
|
||||
})
|
||||
|
||||
ctx := context.New(config.Project{})
|
||||
t.Run("cmd failed", func(t *testing.T) {
|
||||
require.EqualError(
|
||||
t,
|
||||
shell.Run(context.New(config.Project{}), "", []string{"sh", "-c", "exit 1"}, []string{}, false),
|
||||
`failed to run 'sh -c exit 1': exit status 1`,
|
||||
)
|
||||
})
|
||||
|
||||
err := shell.Run(ctx, "", []string{"echo", "test"}, []string{})
|
||||
assert.NoError(err)
|
||||
}
|
||||
|
||||
func TestRunAnInValidCommand(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
ctx := context.New(config.Project{})
|
||||
|
||||
err := shell.Run(ctx, "", []string{"invalid", "command"}, []string{})
|
||||
|
||||
assert.Error(err)
|
||||
assert.Contains(err.Error(), "executable file not found")
|
||||
t.Run("cmd with output", func(t *testing.T) {
|
||||
require.EqualError(
|
||||
t,
|
||||
shell.Run(context.New(config.Project{}), "", []string{"sh", "-c", `echo something; exit 1`}, []string{}, true),
|
||||
`failed to run 'sh -c echo something; exit 1': exit status 1`,
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("with env and dir", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
require.NoError(t, shell.Run(context.New(config.Project{}), dir, []string{"sh", "-c", "touch $FOO"}, []string{"FOO=bar"}, false))
|
||||
require.FileExists(t, filepath.Join(dir, "bar"))
|
||||
})
|
||||
}
|
||||
|
@ -372,9 +372,10 @@ func (bhc Hooks) JSONSchemaType() *jsonschema.Type {
|
||||
}
|
||||
|
||||
type Hook struct {
|
||||
Dir string `yaml:"dir,omitempty"`
|
||||
Cmd string `yaml:"cmd,omitempty"`
|
||||
Env []string `yaml:"env,omitempty"`
|
||||
Dir string `yaml:"dir,omitempty"`
|
||||
Cmd string `yaml:"cmd,omitempty"`
|
||||
Env []string `yaml:"env,omitempty"`
|
||||
Output bool `yaml:"output,omitempty"`
|
||||
}
|
||||
|
||||
// UnmarshalYAML is a custom unmarshaler that allows simplified declarations of commands as strings.
|
||||
|
@ -277,6 +277,7 @@ builds:
|
||||
pre:
|
||||
- cmd: first-script.sh
|
||||
dir: "{{ dir .Dist}}"
|
||||
output: true # always print command output, otherwise only visible in debug mode
|
||||
env:
|
||||
- HOOK_SPECIFIC_VAR={{ .Env.GLOBAL_VAR }}
|
||||
- second-script.sh
|
||||
|
@ -38,6 +38,7 @@ GoReleaser allows this with the global hooks feature.
|
||||
- make clean # simple string
|
||||
- cmd: go generate ./... # specify cmd
|
||||
- cmd: go mod tidy
|
||||
output: true # always prints command output
|
||||
dir: ./submodule # specify command working directory
|
||||
- cmd: touch {{ .Env.FILE_TO_TOUCH }}
|
||||
env:
|
||||
|
Loading…
Reference in New Issue
Block a user