-
Notifications
You must be signed in to change notification settings - Fork 17
/
env.go
48 lines (41 loc) · 1.04 KB
/
env.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
43
44
45
46
47
48
package procs
import (
"fmt"
"os"
"strings"
)
// ParseEnv takes an environment []string and converts it to a map[string]string.
func ParseEnv(environ []string) map[string]string {
env := make(map[string]string)
for _, e := range environ {
pair := strings.SplitN(e, "=", 2)
// There is a chance we can get an env with empty values
if len(pair) == 2 {
env[pair[0]] = pair[1]
}
}
return env
}
// Env takes a map[string]string and converts it to a []string that
// can be used with exec.Cmd. The useEnv boolean flag will include the
// current process environment, overlaying the provided env
// map[string]string.
func Env(env map[string]string, useEnv bool) []string {
envlist := []string{}
// update our env by loading our env and overriding any values in
// the provided env.
if useEnv {
environ := ParseEnv(os.Environ())
for k, v := range env {
environ[k] = v
}
env = environ
}
for key, val := range env {
if key == "" {
continue
}
envlist = append(envlist, fmt.Sprintf("%s=%s", key, val))
}
return envlist
}