1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-09 13:36:56 +02:00

style: avoid bad abstractions

This commit is contained in:
Carlos Alexandro Becker 2018-01-26 19:26:28 -02:00
parent 9897a001bd
commit a2d8087661
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
4 changed files with 34 additions and 38 deletions

View File

@ -2,12 +2,8 @@
package build
import (
"errors"
"os"
"os/exec"
"sync"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
)
@ -39,19 +35,3 @@ type Builder interface {
Default(build config.Build) config.Build
Build(ctx *context.Context, build config.Build, options Options) error
}
// Run runs a command within the given context and env
func Run(ctx *context.Context, command, env []string) error {
/* #nosec */
var cmd = exec.CommandContext(ctx, command[0], command[1:]...)
var log = log.WithField("env", env).WithField("cmd", command)
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Env = append(cmd.Env, env...)
// TODO: improve debug here
log.Debug("running")
if out, err := cmd.CombinedOutput(); err != nil {
log.WithError(err).Debug("failed")
return errors.New(string(out))
}
return nil
}

View File

@ -24,19 +24,3 @@ func TestRegisterAndGet(t *testing.T) {
Register("dummy", builder)
assert.Equal(t, builder, For("dummy"))
}
func TestRun(t *testing.T) {
assert.NoError(t, Run(
context.New(config.Project{}),
[]string{"go", "list", "./..."},
emptyEnv,
))
}
func TestRunInvalidCommand(t *testing.T) {
assert.Error(t, Run(
context.New(config.Project{}),
[]string{"gggggo", "nope"},
emptyEnv,
))
}

View File

@ -7,10 +7,12 @@ import (
"go/parser"
"go/token"
"os"
"os/exec"
"strings"
"text/template"
"time"
"github.com/apex/log"
api "github.com/goreleaser/goreleaser/build"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
@ -67,7 +69,7 @@ func (*Builder) Build(ctx *context.Context, build config.Build, options api.Opti
cmd = append(cmd, "-ldflags="+flags, "-o", options.Path, build.Main)
var target = newBuildTarget(options.Target)
var env = append(build.Env, target.Env()...)
if err := api.Run(ctx, cmd, env); err != nil {
if err := run(ctx, cmd, env); err != nil {
return errors.Wrapf(err, "failed to build for %s", options.Target)
}
ctx.Artifacts.Add(artifact.Artifact{
@ -110,6 +112,20 @@ func ldflags(ctx *context.Context, build config.Build) (string, error) {
return out.String(), err
}
func run(ctx *context.Context, command, env []string) error {
/* #nosec */
var cmd = exec.CommandContext(ctx, command[0], command[1:]...)
var log = log.WithField("env", env).WithField("cmd", command)
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Env = append(cmd.Env, env...)
log.WithField("cmd", command).WithField("env", env).Debug("running")
if out, err := cmd.CombinedOutput(); err != nil {
log.WithError(err).Debug("failed")
return errors.New(string(out))
}
return nil
}
type buildTarget struct {
os, arch, arm string
}

View File

@ -3,6 +3,8 @@
package build
import (
"os"
"os/exec"
"path/filepath"
"strings"
@ -88,7 +90,7 @@ func runHook(ctx *context.Context, env []string, hook string) error {
}
log.WithField("hook", hook).Info("running hook")
cmd := strings.Fields(hook)
return builders.Run(ctx, cmd, env)
return run(ctx, cmd, env)
}
func doBuild(ctx *context.Context, build config.Build, target string) error {
@ -110,3 +112,17 @@ func extFor(target string) string {
}
return ""
}
func run(ctx *context.Context, command, env []string) error {
/* #nosec */
var cmd = exec.CommandContext(ctx, command[0], command[1:]...)
var log = log.WithField("env", env).WithField("cmd", command)
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Env = append(cmd.Env, env...)
log.WithField("cmd", command).WithField("env", env).Debug("running")
if out, err := cmd.CombinedOutput(); err != nil {
log.WithError(err).Debug("failed")
return errors.New(string(out))
}
return nil
}