netbird/cmd/root.go

70 lines
1.7 KiB
Go
Raw Normal View History

2021-05-01 12:45:37 +02:00
package cmd
import (
"fmt"
log "github.com/sirupsen/logrus"
2021-05-01 12:45:37 +02:00
"github.com/spf13/cobra"
"os"
"os/signal"
"runtime"
2021-05-01 12:45:37 +02:00
)
const (
// ExitSetupFailed defines exit code
ExitSetupFailed = 1
)
2021-05-01 12:45:37 +02:00
var (
configPath string
defaultConfigPath string
logLevel string
2021-05-01 12:45:37 +02:00
rootCmd = &cobra.Command{
Use: "wiretrustee",
Short: "",
Long: "",
}
)
// Execute executes the root command.
func Execute() error {
return rootCmd.Execute()
}
func init() {
defaultConfigPath = "/etc/wiretrustee/config.json"
if runtime.GOOS == "windows" {
2021-06-20 23:33:49 +02:00
defaultConfigPath = os.Getenv("PROGRAMDATA") + "\\Wiretrustee\\" + "config.json"
}
rootCmd.PersistentFlags().StringVar(&configPath, "config", defaultConfigPath, "Wiretrustee config file location to write new config to")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "")
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(addPeerCmd)
2021-05-01 12:45:37 +02:00
rootCmd.AddCommand(upCmd)
rootCmd.AddCommand(signalCmd)
2021-06-19 14:55:45 +02:00
rootCmd.AddCommand(serviceCmd)
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
}
// 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)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
fmt.Println("\r- Ctrl+C pressed in Terminal")
os.Exit(0)
}
}()
2021-05-01 12:45:37 +02:00
}
// InitLog parses and sets log-level input
func InitLog(logLevel string) {
level, err := log.ParseLevel(logLevel)
if err != nil {
log.Errorf("Failed parsing log-level %s: %s", logLevel, err)
os.Exit(ExitSetupFailed)
}
log.SetLevel(level)
}