-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (51 loc) · 1.05 KB
/
main.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
package main
import (
"context"
"fmt"
"os"
"os/exec"
"strconv"
"syscall"
"time"
)
func main() {
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr,
"Usage: %s <timeout-in-sec> <command ...>\n", os.Args[0])
os.Exit(1)
}
timeout, err := strconv.ParseUint(os.Args[1], 10, 32)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
ctx, _ := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
cmd := exec.CommandContext(ctx, os.Args[2], os.Args[3:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
err = cmd.Wait()
if err != nil {
if ctx.Err() != nil {
os.Exit(124)
} else {
exitErr, ok := err.(*exec.ExitError)
if !ok {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(255)
}
waits, ok := exitErr.Sys().(syscall.WaitStatus)
if !ok {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(255)
}
os.Exit(waits.ExitStatus())
}
} else {
os.Exit(0)
}
}