Simple implementation of the tillerless mode (#531)

Ref #449
This commit is contained in:
Patrick Valsecchi
2019-04-05 19:02:37 +09:00
committed by KUOKA Yusuke
parent 72c43a2f46
commit 1acd07fa7e
13 changed files with 312 additions and 118 deletions
+30 -2
View File
@@ -1,7 +1,9 @@
package helmexec
import (
"os"
"os/exec"
"strings"
)
const (
@@ -11,7 +13,7 @@ const (
// Runner interface for shell commands
type Runner interface {
Execute(cmd string, args []string) ([]byte, error)
Execute(cmd string, args []string, env map[string]string) ([]byte, error)
}
// ShellRunner implemention for shell commands
@@ -20,8 +22,34 @@ type ShellRunner struct {
}
// Execute a shell command
func (shell ShellRunner) Execute(cmd string, args []string) ([]byte, error) {
func (shell ShellRunner) Execute(cmd string, args []string, env map[string]string) ([]byte, error) {
preparedCmd := exec.Command(cmd, args...)
preparedCmd.Dir = shell.Dir
preparedCmd.Env = mergeEnv(os.Environ(), env)
return preparedCmd.CombinedOutput()
}
func mergeEnv(orig []string, new map[string]string) []string {
wanted := env2map(orig)
for k, v := range new {
wanted[k] = v
}
return map2env(wanted)
}
func map2env(wanted map[string]string) []string {
result := []string{}
for k, v := range wanted {
result = append(result, k+"="+v)
}
return result
}
func env2map(env []string) map[string]string {
wanted := map[string]string{}
for _, cur := range env {
pair := strings.SplitN(cur, "=", 2)
wanted[pair[0]] = pair[1]
}
return wanted
}