-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
73 lines (57 loc) · 1.36 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
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
)
func getProcessName(args []string) string {
return args[0]
}
func StartProcess(processName string, processArgs []string, extraEnv []string) *exec.Cmd {
cmd := exec.Command(processName, processArgs...)
if extraEnv != nil {
cmd.Env = append(os.Environ(), extraEnv...)
// #2: Support for setting workdir (PWD)
for _, envAsStr := range extraEnv {
if strings.Contains(envAsStr, "PWD=") {
name, value := parseEnvironmentVariable(envAsStr)
_ = os.Setenv(name, value)
cmd.Dir = os.Getenv("PWD")
break
}
}
}
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
log.Errorf("Cannot start process: %v", err)
os.Exit(10)
}
return cmd
}
func waitAndRetrieveStatusCode(cmd *exec.Cmd) int {
code := 0
if err := cmd.Wait(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
code = status.ExitStatus()
}
}
}
return code
}
func saveExitCode(code int, path string) error {
dir := filepath.Dir(path)
if _, err := os.Stat(dir); !os.IsNotExist(err) {
_ = os.MkdirAll(dir, 0644)
}
return os.WriteFile(path, []byte(fmt.Sprintf("%v", code)), 0644)
}
func exit(code int) {
os.Exit(code)
}