Skip to content

fixed a bug with environment vars being passed to targets #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion executor/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions executor/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
20 changes: 13 additions & 7 deletions task/target.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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:]...)
}
Expand All @@ -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
}
31 changes: 31 additions & 0 deletions task/target_test.go
Original file line number Diff line number Diff line change
@@ -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)
}