generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wait_signal.go
63 lines (55 loc) · 1.12 KB
/
wait_signal.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
package main
import (
"fmt"
"io"
"os"
osSignal "os/signal"
)
func waitSignal(stdout io.Writer, args []string, exit exitFunc) {
var sig []os.Signal
var message = ""
var err error
loop:
for {
if len(args) == 0 {
break loop
}
switch args[0] {
case "--message":
if len(message) > 0 {
usage("--message is duplicate", true, 1, exit)
}
message = args[1]
args = args[2:]
case "--signal":
if len(args) < 2 {
usage("--signal requires an argument", true, 1, exit)
}
signalName := args[1]
var signalObject os.Signal
args = args[2:]
signalObject, err = processSignalName(signalName)
if err != nil {
usage(err.Error(), true, 1, exit)
}
sig = append(sig, signalObject)
default:
usage(fmt.Sprintf("unknown option: %s", args[0]), true, 1, exit)
}
}
if len(sig) == 0 {
usage("missing required --signal option", true, 1, exit)
}
chanSig := make(chan os.Signal, 1)
chanDone := make(chan struct{})
osSignal.Notify(chanSig, sig...)
go func() {
<-chanSig
chanDone <- struct{}{}
}()
<-chanDone
if len(message) > 0 {
_, _ = stdout.Write([]byte(message))
}
exit(0)
}