Skip to content

Commit

Permalink
task: override env vars on exec
Browse files Browse the repository at this point in the history
syscall.Exec does not override env vars, it just seems to send all
env vars, even duplicates. This fixes the issue.
  • Loading branch information
yields committed Aug 31, 2019
1 parent 31a80e4 commit 3deb296
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"

"github.com/mattn/go-shellwords"
Expand Down Expand Up @@ -93,7 +94,33 @@ func (t *Task) RunExec(args []string) error {
return err
}

env := append(os.Environ(), t.Env...)
env := merge(os.Environ(), t.Env)
args = append(fields, args...)
return syscall.Exec(path, args, env)
}

// Merge merges the given two lists of env vars.
func merge(a, b []string) []string {
var items = make(map[string]string)
var ret []string

for _, item := range a {
if i := strings.Index(item, "="); i != -1 {
key := item[:i]
items[key] = item[i+1:]
}
}

for _, item := range b {
if i := strings.Index(item, "="); i != -1 {
key := item[:i]
items[key] = item[i+1:]
}
}

for k, v := range items {
ret = append(ret, k+"="+v)
}

return ret
}

0 comments on commit 3deb296

Please sign in to comment.