-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
62 lines (48 loc) · 991 Bytes
/
process.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
package main
import (
"context"
"os"
"os/exec"
)
type Process struct {
*exec.Cmd
configSources []ConfigSource
}
func NewProcess(name string, arg ...string) *Process {
p := new(Process)
p.Cmd = exec.Command(name, arg...)
// Defaults
p.Stdin = os.Stdin
p.Stdout = os.Stdout
p.Stderr = os.Stderr
return p
}
func (p *Process) AppendConfigSource(sources ...ConfigSource) {
p.configSources = append(p.configSources, sources...)
}
func (p *Process) resetAndInstallEnv() error {
// Reset
p.Env = os.Environ()
for _, configSource := range p.configSources {
items, err := configSource.List(context.Background())
if err != nil {
return err
}
p.Env = append(p.Env, items...)
}
return nil
}
func (p *Process) Start() error {
var err error
err = p.resetAndInstallEnv()
if err != nil {
return err
}
return p.Cmd.Start()
}
func (p *Process) Stop() error {
return p.Cmd.Process.Signal(os.Interrupt)
}
func (p *Process) Wait() error {
return p.Cmd.Wait()
}