2019-12-01 21:58:12 +00:00
|
|
|
package cli
|
|
|
|
|
2019-12-01 22:53:50 +00:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
2019-12-17 22:41:46 +00:00
|
|
|
"os/signal"
|
2019-12-01 22:53:50 +00:00
|
|
|
"strconv"
|
2019-12-17 22:41:46 +00:00
|
|
|
"syscall"
|
2019-12-01 22:53:50 +00:00
|
|
|
)
|
2019-12-01 21:58:12 +00:00
|
|
|
|
|
|
|
func SetDefaultValueFromEnv(value *string, key string, defaultValue string) {
|
|
|
|
if os.Getenv(key) != "" {
|
|
|
|
*value = os.Getenv(key)
|
|
|
|
} else {
|
|
|
|
*value = defaultValue
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 22:53:50 +00:00
|
|
|
func SetIntDefaultValueFromEnv(value *int, key string, defaultValue int) error {
|
|
|
|
var sVal string
|
|
|
|
if os.Getenv(key) != "" {
|
|
|
|
sVal = os.Getenv(key)
|
|
|
|
val, err := strconv.Atoi(sVal)
|
2019-12-14 09:43:02 +00:00
|
|
|
if err != nil {
|
2019-12-01 22:53:50 +00:00
|
|
|
log.Printf("unable to convert string to int: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*value = val
|
|
|
|
} else {
|
|
|
|
*value = defaultValue
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-17 22:41:46 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}()
|
|
|
|
}
|