diff --git a/executor/cmd.go b/executor/cmd.go index 4da8bf4..9b81bf9 100644 --- a/executor/cmd.go +++ b/executor/cmd.go @@ -103,7 +103,7 @@ func (e *CommandExecutor) execute( zap.Strings("cmd", target.Up), zap.String("url", target.RepoURL), zap.String("dir", path), - zap.Int("env", len(ex.env)), + zap.Any("env", ex.env), zap.Bool("passthrough", e.passEnvironment)) return target.Execute(ex.path, ex.env, ex.shutdown, ex.passEnvironment) diff --git a/executor/cmd_test.go b/executor/cmd_test.go index db8661a..d9e77f7 100644 --- a/executor/cmd_test.go +++ b/executor/cmd_test.go @@ -67,12 +67,15 @@ func TestCommandPrepareWithoutPassthrough(t *testing.T) { }, }, false, "pico", "GLOBAL_") - ex, err := ce.prepare("test", "./", false, nil) + ex, err := ce.prepare("test", "./", false, map[string]string{ + "DATA_DIR": "/data/shared", + }) assert.NoError(t, err) assert.Equal(t, exec{ path: "./", env: map[string]string{ "SOME_SECRET": "123", + "DATA_DIR": "/data/shared", }, shutdown: false, passEnvironment: false, @@ -92,13 +95,16 @@ func TestCommandPrepareWithGlobal(t *testing.T) { }, }, false, "pico", "GLOBAL_") - ex, err := ce.prepare("test", "./", false, nil) + ex, err := ce.prepare("test", "./", false, map[string]string{ + "DATA_DIR": "/data/shared", + }) assert.NoError(t, err) assert.Equal(t, exec{ path: "./", env: map[string]string{ "SOME_SECRET": "123", "SECRET": "456", + "DATA_DIR": "/data/shared", }, shutdown: false, passEnvironment: false, diff --git a/task/target.go b/task/target.go index 15d5585..bfefe40 100644 --- a/task/target.go +++ b/task/target.go @@ -1,10 +1,11 @@ package task import ( - "errors" "fmt" "os" "os/exec" + + "github.com/pkg/errors" ) // ExecutionTask encodes a Target with additional execution-time information. @@ -70,15 +71,20 @@ func (t *Target) Execute(dir string, env map[string]string, shutdown bool, inher command = t.Up } - return execute(dir, env, command, inheritEnv) + c, err := prepare(dir, env, command, inheritEnv) + if err != nil { + return errors.Wrap(err, "failed to prepare command for execution") + } + + return c.Run() } -func execute(dir string, env map[string]string, command []string, inheritEnv bool) (err error) { +func prepare(dir string, env map[string]string, command []string, inheritEnv bool) (cmd *exec.Cmd, err error) { if len(command) == 0 { - return errors.New("attempt to execute target with empty command") + return nil, errors.New("attempt to execute target with empty command") } - cmd := exec.Command(command[0]) + cmd = exec.Command(command[0]) if len(command) > 1 { cmd.Args = append(cmd.Args, command[1:]...) } @@ -91,9 +97,9 @@ func execute(dir string, env map[string]string, command []string, inheritEnv boo cmdEnv = os.Environ() } for k, v := range env { - cmdEnv = append(cmd.Env, fmt.Sprintf("%s=%s", k, v)) + cmdEnv = append(cmdEnv, fmt.Sprintf("%s=%s", k, v)) } cmd.Env = cmdEnv - return cmd.Run() + return cmd, nil } diff --git a/task/target_test.go b/task/target_test.go new file mode 100644 index 0000000..278b4ba --- /dev/null +++ b/task/target_test.go @@ -0,0 +1,31 @@ +package task + +import ( + "sort" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPrepareTargetExecution(t *testing.T) { + c, err := prepare(".", map[string]string{ + "VAR_1": "one", + "VAR_2": "two", + "VAR_3": "three", + "VAR_4": "four", + }, []string{"docker-compose", "up", "-d"}, false) + assert.NoError(t, err) + + assert.Equal(t, []string{"docker-compose", "up", "-d"}, c.Args) + want := []string{ + "VAR_1=one", + "VAR_2=two", + "VAR_3=three", + "VAR_4=four", + } + got := c.Env + sort.Strings(want) + sort.Strings(got) + assert.Equal(t, want, got) + assert.Equal(t, ".", c.Dir) +}