Browse Source

Merge pull request #960 from gravitl/feature_v0.12.2_client_verbosity

reworked usage/description for netclient and added verbosity flags
dcarns 3 years ago
parent
commit
a6b1c6a8cd

+ 6 - 0
logger/util.go

@@ -6,6 +6,9 @@ import (
 	"strings"
 	"strings"
 )
 )
 
 
+// Verbosity - current logging verbosity level (optionally set)
+var Verbosity = 0
+
 // MakeString - makes a string using golang string builder
 // MakeString - makes a string using golang string builder
 func MakeString(delimeter string, message ...string) string {
 func MakeString(delimeter string, message ...string) string {
 	var builder strings.Builder
 	var builder strings.Builder
@@ -19,6 +22,9 @@ func MakeString(delimeter string, message ...string) string {
 }
 }
 
 
 func getVerbose() int32 {
 func getVerbose() int32 {
+	if Verbosity >= 1 && Verbosity <= 3 {
+		return int32(Verbosity)
+	}
 	level, err := strconv.Atoi(os.Getenv("VERBOSITY"))
 	level, err := strconv.Atoi(os.Getenv("VERBOSITY"))
 	if err != nil || level < 0 {
 	if err != nil || level < 0 {
 		level = 0
 		level = 0

+ 20 - 0
netclient/cli_options/cmds.go

@@ -3,6 +3,7 @@ package cli_options
 import (
 import (
 	"errors"
 	"errors"
 
 
+	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/netclient/command"
 	"github.com/gravitl/netmaker/netclient/command"
 	"github.com/gravitl/netmaker/netclient/config"
 	"github.com/gravitl/netmaker/netclient/config"
 	"github.com/urfave/cli/v2"
 	"github.com/urfave/cli/v2"
@@ -16,6 +17,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command {
 			Usage: "Join a Netmaker network.",
 			Usage: "Join a Netmaker network.",
 			Flags: cliFlags,
 			Flags: cliFlags,
 			Action: func(c *cli.Context) error {
 			Action: func(c *cli.Context) error {
+				parseVerbosity(c)
 				cfg, pvtKey, err := config.GetCLIConfig(c)
 				cfg, pvtKey, err := config.GetCLIConfig(c)
 				if err != nil {
 				if err != nil {
 					return err
 					return err
@@ -39,6 +41,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command {
 			// the action, or code that will be executed when
 			// the action, or code that will be executed when
 			// we execute our `ns` command
 			// we execute our `ns` command
 			Action: func(c *cli.Context) error {
 			Action: func(c *cli.Context) error {
+				parseVerbosity(c)
 				cfg, _, err := config.GetCLIConfig(c)
 				cfg, _, err := config.GetCLIConfig(c)
 				if err != nil {
 				if err != nil {
 					return err
 					return err
@@ -54,6 +57,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command {
 			// the action, or code that will be executed when
 			// the action, or code that will be executed when
 			// we execute our `ns` command
 			// we execute our `ns` command
 			Action: func(c *cli.Context) error {
 			Action: func(c *cli.Context) error {
+				parseVerbosity(c)
 				cfg, _, err := config.GetCLIConfig(c)
 				cfg, _, err := config.GetCLIConfig(c)
 				if err != nil {
 				if err != nil {
 					return err
 					return err
@@ -69,6 +73,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command {
 			// the action, or code that will be executed when
 			// the action, or code that will be executed when
 			// we execute our `ns` command
 			// we execute our `ns` command
 			Action: func(c *cli.Context) error {
 			Action: func(c *cli.Context) error {
+				parseVerbosity(c)
 				cfg, _, err := config.GetCLIConfig(c)
 				cfg, _, err := config.GetCLIConfig(c)
 				if err != nil {
 				if err != nil {
 					return err
 					return err
@@ -84,6 +89,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command {
 			// the action, or code that will be executed when
 			// the action, or code that will be executed when
 			// we execute our `ns` command
 			// we execute our `ns` command
 			Action: func(c *cli.Context) error {
 			Action: func(c *cli.Context) error {
+				parseVerbosity(c)
 				err := command.Uninstall()
 				err := command.Uninstall()
 				return err
 				return err
 			},
 			},
@@ -93,9 +99,23 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command {
 			Usage: "run netclient as daemon",
 			Usage: "run netclient as daemon",
 			Flags: cliFlags,
 			Flags: cliFlags,
 			Action: func(c *cli.Context) error {
 			Action: func(c *cli.Context) error {
+				// set max verbosity for daemon regardless
+				logger.Verbosity = 3
 				err := command.Daemon()
 				err := command.Daemon()
 				return err
 				return err
 			},
 			},
 		},
 		},
 	}
 	}
 }
 }
+
+// == Private funcs ==
+
+func parseVerbosity(c *cli.Context) {
+	if c.Bool("v") {
+		logger.Verbosity = 1
+	} else if c.Bool("vv") {
+		logger.Verbosity = 2
+	} else if c.Bool("vvv") {
+		logger.Verbosity = 3
+	}
+}

+ 18 - 0
netclient/cli_options/flags.go

@@ -204,5 +204,23 @@ func GetFlags(hostname string) []cli.Flag {
 			Value:   "no",
 			Value:   "no",
 			Usage:   "Allows to run the command with force, if otherwise prevented.",
 			Usage:   "Allows to run the command with force, if otherwise prevented.",
 		},
 		},
+		&cli.BoolFlag{
+			Name:    "verbosity-level-1",
+			Aliases: []string{"v"},
+			Value:   false,
+			Usage:   "Netclient Verbosity level 1.",
+		},
+		&cli.BoolFlag{
+			Name:    "verbosity-level-2",
+			Aliases: []string{"vv"},
+			Value:   false,
+			Usage:   "Netclient Verbosity level 2.",
+		},
+		&cli.BoolFlag{
+			Name:    "verbosity-level-3",
+			Aliases: []string{"vvv"},
+			Value:   false,
+			Usage:   "Netclient Verbosity level 3.",
+		},
 	}
 	}
 }
 }

+ 0 - 1
netclient/config/config.go

@@ -25,7 +25,6 @@ type ClientConfig struct {
 	Daemon          string         `yaml:"daemon"`
 	Daemon          string         `yaml:"daemon"`
 	OperatingSystem string         `yaml:"operatingsystem"`
 	OperatingSystem string         `yaml:"operatingsystem"`
 	DebugOn         bool           `yaml:"debugon"`
 	DebugOn         bool           `yaml:"debugon"`
-	
 }
 }
 
 
 // ServerConfig - struct for dealing with the server information for a netclient
 // ServerConfig - struct for dealing with the server information for a netclient

+ 4 - 3
netclient/main.go

@@ -17,13 +17,14 @@ var version = "dev"
 
 
 func main() {
 func main() {
 	app := cli.NewApp()
 	app := cli.NewApp()
-	app.Name = "Netclient CLI"
-	app.Usage = "Netmaker's netclient agent and CLI. Used to perform interactions with Netmaker server and set local WireGuard config."
+	app.Name = "Netclient"
 	app.Version = version
 	app.Version = version
 	ncutils.SetVersion(version)
 	ncutils.SetVersion(version)
-
 	cliFlags := cli_options.GetFlags(ncutils.GetHostname())
 	cliFlags := cli_options.GetFlags(ncutils.GetHostname())
 	app.Commands = cli_options.GetCommands(cliFlags[:])
 	app.Commands = cli_options.GetCommands(cliFlags[:])
+	app.Description = "Used to perform interactions with Netmaker server and set local WireGuard config."
+	app.Usage = "Netmaker's netclient agent and CLI."
+	app.UsageText = "netclient [global options] command [command options] [arguments...]. Adjust verbosity of given command with -v, -vv or -vvv (max)."
 
 
 	setGarbageCollection()
 	setGarbageCollection()