2021-05-01 12:45:37 +02:00
package cmd
import (
"fmt"
2021-10-17 21:34:07 +02:00
log "github.com/sirupsen/logrus"
2021-09-07 09:53:18 +02:00
"github.com/spf13/cobra"
2021-11-15 09:11:50 +01:00
"github.com/spf13/pflag"
2021-08-18 13:35:42 +02:00
"github.com/wiretrustee/wiretrustee/client/internal"
2021-05-01 12:45:37 +02:00
"os"
"os/signal"
2021-06-20 23:01:44 +02:00
"runtime"
2021-11-15 09:11:50 +01:00
"strings"
2021-10-17 21:34:07 +02:00
"syscall"
2021-05-01 12:45:37 +02:00
)
var (
2021-06-20 23:01:44 +02:00
configPath string
defaultConfigPath string
logLevel string
2021-09-07 09:53:18 +02:00
defaultLogFile string
logFile string
2021-08-18 13:35:42 +02:00
managementURL string
2021-11-15 09:11:50 +01:00
setupKey string
2021-10-17 21:34:07 +02:00
rootCmd = & cobra . Command {
2021-05-01 12:45:37 +02:00
Use : "wiretrustee" ,
Short : "" ,
Long : "" ,
}
2021-07-17 14:38:59 +02:00
// Execution control channel for stopCh signal
2021-10-17 22:15:38 +02:00
stopCh chan int
cleanupCh chan struct { }
2021-05-01 12:45:37 +02:00
)
// Execute executes the root command.
func Execute ( ) error {
return rootCmd . Execute ( )
}
func init ( ) {
2021-07-17 14:38:59 +02:00
stopCh = make ( chan int )
2021-10-17 22:15:38 +02:00
cleanupCh = make ( chan struct { } )
2021-07-17 14:38:59 +02:00
2021-06-20 23:01:44 +02:00
defaultConfigPath = "/etc/wiretrustee/config.json"
2021-09-07 09:53:18 +02:00
defaultLogFile = "/var/log/wiretrustee/client.log"
2021-06-20 23:01:44 +02:00
if runtime . GOOS == "windows" {
2021-06-20 23:33:49 +02:00
defaultConfigPath = os . Getenv ( "PROGRAMDATA" ) + "\\Wiretrustee\\" + "config.json"
2021-09-07 09:53:18 +02:00
defaultLogFile = os . Getenv ( "PROGRAMDATA" ) + "\\Wiretrustee\\" + "client.log"
2021-06-20 23:01:44 +02:00
}
2021-08-18 13:35:42 +02:00
rootCmd . PersistentFlags ( ) . StringVar ( & managementURL , "management-url" , "" , fmt . Sprintf ( "Management Service URL [http|https]://[host]:[port] (default \"%s\")" , internal . ManagementURLDefault ( ) . String ( ) ) )
rootCmd . PersistentFlags ( ) . StringVar ( & configPath , "config" , defaultConfigPath , "Wiretrustee config file location" )
rootCmd . PersistentFlags ( ) . StringVar ( & logLevel , "log-level" , "info" , "sets Wiretrustee log level" )
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-10-17 21:34:07 +02:00
rootCmd . PersistentFlags ( ) . StringVar ( & setupKey , "setup-key" , "" , "Setup key obtained from the Management Service Dashboard (used to register peer)" )
2021-06-19 14:55:45 +02:00
rootCmd . AddCommand ( serviceCmd )
2021-08-15 16:56:26 +02:00
rootCmd . AddCommand ( upCmd )
2021-08-18 13:35:42 +02:00
rootCmd . AddCommand ( loginCmd )
2021-10-17 21:34:07 +02:00
rootCmd . AddCommand ( versionCmd )
2021-06-20 23:01:44 +02:00
serviceCmd . AddCommand ( runCmd , startCmd , stopCmd , restartCmd ) // service control commands are subcommands of service
serviceCmd . AddCommand ( installCmd , uninstallCmd ) // service installer commands are subcommands of service
2021-05-01 12:45:37 +02:00
}
2021-05-15 12:33:07 +02:00
// SetupCloseHandler handles SIGTERM signal and exits with success
2021-05-01 12:45:37 +02:00
func SetupCloseHandler ( ) {
2021-05-19 11:13:25 +02:00
c := make ( chan os . Signal , 1 )
2021-10-17 21:34:07 +02:00
signal . Notify ( c , os . Interrupt , syscall . SIGINT , syscall . SIGTERM )
2021-05-19 11:13:25 +02:00
go func ( ) {
for range c {
2021-10-17 21:34:07 +02:00
log . Info ( "shutdown signal received" )
2021-07-17 14:38:59 +02:00
stopCh <- 0
2021-05-19 11:13:25 +02:00
}
} ( )
2021-05-01 12:45:37 +02:00
}
2021-11-15 09:11:50 +01:00
// SetFlagsFromEnvVars reads and updates flag values from environment variables with prefix WT_
func SetFlagsFromEnvVars ( ) {
flags := rootCmd . PersistentFlags ( )
flags . VisitAll ( func ( f * pflag . Flag ) {
envVar := FlagNameToEnvVar ( f . Name )
if value , present := os . LookupEnv ( envVar ) ; present {
err := flags . Set ( f . Name , value )
if err != nil {
log . Infof ( "unable to configure flag %s using variable %s, err: %v" , f . Name , envVar , err )
}
}
} )
}
// FlagNameToEnvVar converts flag name to environment var name adding a prefix,
// replacing dashes and making all uppercase (e.g. setup-keys is converted to WT_SETUP_KEYS)
func FlagNameToEnvVar ( f string ) string {
prefix := "WT_"
parsed := strings . ReplaceAll ( f , "-" , "_" )
upper := strings . ToUpper ( parsed )
return prefix + upper
}