2021-08-13 08:46:30 +02:00
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"os"
"os/signal"
2021-09-07 09:53:18 +02:00
"runtime"
2021-08-13 08:46:30 +02:00
)
const (
// ExitSetupFailed defines exit code
ExitSetupFailed = 1
)
var (
2021-09-07 09:53:18 +02:00
logLevel string
defaultLogFile string
logFile string
2021-08-13 08:46:30 +02:00
rootCmd = & cobra . Command {
Use : "wiretrustee-signal" ,
Short : "" ,
Long : "" ,
}
// Execution control channel for stopCh signal
stopCh chan int
)
// Execute executes the root command.
func Execute ( ) error {
return rootCmd . Execute ( )
}
func init ( ) {
stopCh = make ( chan int )
2021-09-07 09:53:18 +02:00
defaultLogFile = "/var/log/wiretrustee/signal.log"
if runtime . GOOS == "windows" {
defaultLogFile = os . Getenv ( "PROGRAMDATA" ) + "\\Wiretrustee\\" + "signal.log"
}
2021-08-13 08:46:30 +02:00
rootCmd . PersistentFlags ( ) . StringVar ( & logLevel , "log-level" , "info" , "" )
2021-09-07 09:53:18 +02:00
rootCmd . PersistentFlags ( ) . StringVar ( & logFile , "log-file" , defaultLogFile , "sets Wiretrustee log path. If console is specified the the log will be output to stdout" )
2021-08-13 08:46:30 +02:00
rootCmd . AddCommand ( runCmd )
}
// SetupCloseHandler handles SIGTERM signal and exits with success
func SetupCloseHandler ( ) {
c := make ( chan os . Signal , 1 )
signal . Notify ( c , os . Interrupt )
go func ( ) {
for range c {
fmt . Println ( "\r- Ctrl+C pressed in Terminal" )
stopCh <- 0
}
} ( )
}