Utility function to handle exit signal

This commit is contained in:
Cyrille Nofficial 2019-12-17 23:41:46 +01:00
parent e5c81519bf
commit ad0f5e0d0f

View File

@ -3,7 +3,9 @@ package cli
import ( import (
"log" "log"
"os" "os"
"os/signal"
"strconv" "strconv"
"syscall"
) )
func SetDefaultValueFromEnv(value *string, key string, defaultValue string) { func SetDefaultValueFromEnv(value *string, key string, defaultValue string) {
@ -28,3 +30,19 @@ func SetIntDefaultValueFromEnv(value *int, key string, defaultValue int) error {
} }
return nil return nil
} }
type Part interface {
Start() error
Stop()
}
func HandleExit(p Part) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Kill, os.Interrupt, syscall.SIGTERM)
go func() {
<-signals
p.Stop()
os.Exit(0)
}()
}