mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-17 23:58:45 +02:00
committed by
KUOKA Yusuke
parent
72c43a2f46
commit
1acd07fa7e
+30
-2
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user