-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
87 lines (72 loc) · 1.8 KB
/
exec.go
File metadata and controls
87 lines (72 loc) · 1.8 KB
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
package main
import (
"fmt"
"os"
osexec "os/exec"
"os/signal"
"runtime"
"strings"
"syscall"
)
type environ []string
// Unset an environment variable by key
func (e *environ) Unset(key string) {
for i := range *e {
if strings.HasPrefix((*e)[i], key+"=") {
(*e)[i] = (*e)[len(*e)-1]
*e = (*e)[:len(*e)-1]
break
}
}
}
// Set adds an environment variable, replacing any existing ones of the same key
func (e *environ) Set(key, val string) {
e.Unset(key)
*e = append(*e, key+"="+val)
}
func execSyscall(command string, args []string, env []string) error {
argv0, err := osexec.LookPath(command)
if err != nil {
return fmt.Errorf("Couldn't find the executable '%s': %w", command, err)
}
argv := make([]string, 0, 1+len(args))
argv = append(argv, command)
argv = append(argv, args...)
return syscall.Exec(argv0, argv, env)
}
func execCmd(command string, args []string, env []string) error {
cmd := osexec.Command(command, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = env
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan)
if err := cmd.Start(); err != nil {
return err
}
go func() {
for {
sig := <-sigChan
cmd.Process.Signal(sig)
}
}()
if err := cmd.Wait(); err != nil {
cmd.Process.Signal(os.Kill)
return fmt.Errorf("Failed to wait for command termination: %w", err)
}
waitStatus := cmd.ProcessState.Sys().(syscall.WaitStatus)
os.Exit(waitStatus.ExitStatus())
return nil
}
func supportsExecSyscall() bool {
return runtime.GOOS == "linux" || runtime.GOOS == "darwin" || runtime.GOOS == "freebsd"
}
func run(port string, command string, args []string) error {
env := environ(os.Environ())
env.Set("PORT", port)
if supportsExecSyscall() {
return execSyscall(command, args, env)
}
return execCmd(command, args, env)
}