-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeout.go
executable file
·70 lines (60 loc) · 1.25 KB
/
timeout.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
package main
import (
"fmt"
"os"
"strconv"
"time"
"github.com/go-zoox/cli"
"github.com/go-zoox/command"
"github.com/go-zoox/timeout"
)
func main() {
app := cli.NewSingleProgram(&cli.SingleProgramConfig{
Name: "timeout",
Usage: "timeout exec shell script",
Version: Version,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "age",
Usage: "Timeout Age",
Aliases: []string{"a"},
Required: true,
},
&cli.StringFlag{
Name: "exec",
Usage: "Exec Commands",
Aliases: []string{"e"},
Required: true,
},
&cli.StringFlag{
Name: "message",
Usage: "Timeout Message",
Aliases: []string{"m"},
},
},
})
app.Command(func(ctx *cli.Context) error {
maxAgeRaw := ctx.String("age")
script := ctx.String("exec")
message := ctx.String("message")
if message == "" {
message = fmt.Sprintf("timeout to exec: %s", script)
}
maxAge, err := strconv.Atoi(maxAgeRaw)
if err != nil {
return err
}
cmd := &command.Command{
Script: script,
Environment: map[string]string{
"POWER_BY": "GoZoox",
},
}
if err := timeout.Timeout(cmd.Run, time.Duration(maxAge)*time.Second, message); err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
return nil
})
app.Run()
}