-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
60 lines (53 loc) · 1.11 KB
/
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
package gopherpack
import (
"os"
"os/exec"
"strings"
"syscall"
)
func forkProcess(envValues []string) (*os.Process, error) {
// get file path to current binary
filePath, err := exec.LookPath(os.Args[0])
if err != nil {
return nil, err
}
// current dir
dir, err := os.Getwd()
if err != nil {
return nil, err
}
// inherit stdin, stdout and stderr by child process
files := make([]*os.File, 3)
files[syscall.Stdin] = os.Stdin
files[syscall.Stdout] = os.Stdout
files[syscall.Stderr] = os.Stderr
// prepare environment for child process
env := []string{}
// copy current environment vars but remove all existing gopherpack vars if any
for _, curEnvVar := range os.Environ() {
if strings.HasPrefix(curEnvVar, envPrefix) {
continue
}
env = append(env, curEnvVar)
}
// add gopherpack environment vars
env = append(
env,
envValues...,
)
// run child process
childProcess, err := os.StartProcess(
filePath,
os.Args,
&os.ProcAttr{
Dir: dir,
Env: env,
Files: files,
Sys: &syscall.SysProcAttr{},
},
)
if err != nil {
return nil, err
}
return childProcess, nil
}