diff --git a/cli/cli.go b/cli/cli.go index af84946..f1f1bd7 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -3,7 +3,9 @@ package cli import ( "log" "os" + "os/signal" "strconv" + "syscall" ) func SetDefaultValueFromEnv(value *string, key string, defaultValue string) { @@ -28,3 +30,19 @@ func SetIntDefaultValueFromEnv(value *int, key string, defaultValue int) error { } 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) + }() +}