-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexecute.go
More file actions
42 lines (37 loc) · 803 Bytes
/
execute.go
File metadata and controls
42 lines (37 loc) · 803 Bytes
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
package main
import (
"errors"
"os"
"os/exec"
"os/signal"
"syscall"
)
// Execute is used to run a command and print the value in stdout and stderr.
//
// The return value contains the command's exit code.
func Execute(command []string) int {
cmd := exec.Command(command[0], command[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
Log("unable to start " + err.Error())
return -1
}
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
<-sigint
err = cmd.Process.Signal(os.Interrupt)
if err != nil {
Log("unable send interrupt " + err.Error())
}
err = cmd.Wait()
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return exitErr.ExitCode()
}
return -1
}
return 0
}