2021-07-24 16:14:29 +02:00
package cmd
import (
"fmt"
"os"
"os/signal"
"runtime"
2022-05-13 14:11:21 +02:00
"github.com/spf13/cobra"
2021-07-24 16:14:29 +02:00
)
const (
// ExitSetupFailed defines exit code
ExitSetupFailed = 1
)
var (
configPath string
defaultConfigPath string
logLevel string
2021-09-07 09:53:18 +02:00
defaultLogFile string
logFile string
2021-07-24 16:14:29 +02:00
rootCmd = & cobra . Command {
2022-05-13 14:11:21 +02:00
Use : "netbird-mgmt" ,
2021-07-24 16:14:29 +02:00
Short : "" ,
Long : "" ,
}
// Execution control channel for stopCh signal
stopCh chan int
)
// Execute executes the root command.
func Execute ( ) error {
return rootCmd . Execute ( )
}
2022-05-13 14:11:21 +02:00
func init ( ) {
2021-07-24 16:14:29 +02:00
stopCh = make ( chan int )
2022-05-13 14:11:21 +02:00
defaultMgmtDataDir = "/var/lib/netbird/"
defaultConfigPath = "/etc/netbird"
defaultMgmtConfig = defaultConfigPath + "/management.json"
defaultLogFile = "/var/log/netbird/management.log"
2021-07-24 16:14:29 +02:00
if runtime . GOOS == "windows" {
2022-05-13 14:11:21 +02:00
defaultConfigPath = os . Getenv ( "PROGRAMDATA" ) + "\\Netbird\\" + "management.json"
defaultLogFile = os . Getenv ( "PROGRAMDATA" ) + "\\Netbird\\" + "management.log"
2021-07-24 16:14:29 +02:00
}
2022-05-13 14:11:21 +02:00
rootCmd . PersistentFlags ( ) . StringVar ( & configPath , "config" , defaultConfigPath , "Netbird config file location to write new config to" )
2021-07-24 16:14:29 +02:00
rootCmd . PersistentFlags ( ) . StringVar ( & logLevel , "log-level" , "info" , "" )
2022-05-13 14:11:21 +02:00
rootCmd . PersistentFlags ( ) . StringVar ( & logFile , "log-file" , defaultLogFile , "sets Netbird log path. If console is specified the the log will be output to stdout" )
2021-07-24 16:14:29 +02:00
rootCmd . AddCommand ( mgmtCmd )
}
// 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
}
} ( )
}