-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiaas.go
66 lines (56 loc) · 1.46 KB
/
piaas.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
package piaas
import (
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"os"
"os/signal"
)
// Prepare common flags for commands.
func PrepareCommonFlags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "config",
Usage: "Specify the piaas config file name and path. Default to piaasconfig.yml",
Value: "piaasconfig.yml",
},
cli.BoolFlag{
Name: "debug",
Usage: "Print debug messages",
},
}
}
// Below are common option handler.
// Turn on debug logging if -debug was specified
func HandleDebug(c *cli.Context) error {
if c.Bool("debug") == true {
log.Println("Debug is on.")
log.SetLevel(log.DebugLevel)
}
return nil
}
func HandleExitSignal() {
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, os.Kill)
go func() {
sig := <-signalCh
// no longer accept other signals.
close(signalCh)
log.Debugf("Signal received: %s", sig)
// notify all observers.
for _, obs := range exitObservers {
obs(sig)
}
}()
}
var exitObservers []func(os.Signal)
// Add observer to listen to exit signal.
// Set preferredFront to true to make sure the observer is ranked in front.
func SubscribeExitSignal(obs func(os.Signal), preferredFront bool) {
if preferredFront {
log.Debugf("Add an exit observer in the first position")
exitObservers = append([]func(os.Signal){obs}, exitObservers...)
} else {
log.Debugf("Add an exit observer in the last position")
exitObservers = append(exitObservers, obs)
}
}