forked from helmfile/chartify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
42 lines (37 loc) · 858 Bytes
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package chartify
import (
"io"
"os"
"os/exec"
"strings"
)
func RunCommand(cmd string, args []string, dir string, stdout, stderr io.Writer, env map[string]string) error {
command := exec.Command(cmd, args...)
command.Dir = dir
command.Stdout = stdout
command.Stderr = stderr
command.Env = mergeEnv(os.Environ(), env)
return command.Run()
}
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
}