-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
107 lines (89 loc) · 2.72 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package process
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Executes the specified command and waits for it to complete
func Run(command []string, dir *string, env *map[string]string) (error) {
// Print the command prior to running it
log.Print(command)
// If the specified executable is not a filesystem path then perform PATH lookup
executable := command[0]
if filepath.Base(executable) == executable {
if resolved, err := exec.LookPath(executable); err != nil {
return err
} else {
executable = resolved
}
}
// If no working directory was specified then use the current working directory
workingDir := ""
if dir != nil {
workingDir = *dir
} else {
cwd, err := os.Getwd()
if err != nil {
return err
} else {
workingDir = cwd
}
}
// Merge any supplied environment variables with those of the parent process
environment := os.Environ()
if env != nil {
// Gather the list of keys for our supplied environment variables
keys := []string{}
for key := range *env {
keys = append(keys, key)
}
// Prevent duplicate entries by stripping out any existing values for our keys
// (This ensures the underlying operating system doesn't ignore our supplied values as a result of them appearing after the system defaults)
environment = stripKeys(environment, keys)
// Append the supplied environment variables
for k, v := range *env {
environment = append(environment, fmt.Sprintf("%s=%s", k, v))
}
}
// Attempt to start the child process, ensuring it inherits its stdout and stderr streams from the parent
process, err := os.StartProcess(executable, command, &os.ProcAttr{
Dir: workingDir,
Env: environment,
Files: []*os.File{nil, os.Stdout, os.Stderr},
})
// Verify that the child process started successfully
if err != nil {
return err
}
// Wait for the child process to complete
status, err := process.Wait()
if err != nil {
return err
}
// Verify that the child process completed successfully
if status.Success() == false {
return errors.New(fmt.Sprint("Command ", command, " terminated with exit code ", status.ExitCode()))
}
return nil
}
// Strips the specified list of keys from an array of environment variables
func stripKeys(env []string, keys []string) []string {
// Construct a map of our keys for faster searching
keysMap := map[string]bool{}
for _, key := range keys {
keysMap[strings.ToUpper(key)] = true
}
// Strip out any environment variable entries matching our keys
stripped := []string{}
for _, entry := range env {
components := strings.SplitN(entry, "=", 2)
if len(components) == 2 && !keysMap[strings.ToUpper(components[0])] {
stripped = append(stripped, entry)
}
}
return stripped
}