-
Notifications
You must be signed in to change notification settings - Fork 58
/
proc.go
77 lines (69 loc) · 1.24 KB
/
proc.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package job
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
)
type Envs map[string]string
func (e Envs) addIfMissing(k, v string) {
if _, ok := e[k]; !ok {
e[k] = v
}
}
func merge(e, f Envs) Envs {
g := make(Envs)
for k, v := range e {
g[k] = v
}
for k, v := range f {
g[k] = v
}
return g
}
// Proc represents a process
type Proc struct {
Name string
Prog string
Args []string
Envs Envs
IPv4 uint32
PubAddr string
LogDir string
}
func (p Proc) Cmd() *exec.Cmd {
cmd := exec.Command(p.Prog, p.Args...)
cmd.Env = updatedEnv(p.Envs)
return cmd
}
func (p Proc) Script() string {
buf := &bytes.Buffer{}
fmt.Fprintf(buf, "env \\\n")
for k, v := range p.Envs {
fmt.Fprintf(buf, "\t%s=%q \\\n", k, v)
}
fmt.Fprintf(buf, "\t%s \\\n", p.Prog)
for _, a := range p.Args {
fmt.Fprintf(buf, "\t%s \\\n", a)
}
fmt.Fprintf(buf, "\n")
return buf.String()
}
func updatedEnv(newValues Envs) []string {
envMap := make(Envs)
for _, kv := range os.Environ() {
parts := strings.Split(kv, "=")
if len(parts) == 2 {
envMap[parts[0]] = parts[1]
}
}
for k, v := range newValues {
envMap[k] = v
}
var envs []string
for k, v := range envMap {
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
}
return envs
}