From 30a40aaed693f414ba6a46009f98998bacd39078 Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Thu, 24 Mar 2022 11:27:42 -0400 Subject: [PATCH 1/6] reworked usage/description for netclient and added verbosity flags --- netclient/cli_options/cmds.go | 18 ++++++++++++++++++ netclient/cli_options/flags.go | 18 ++++++++++++++++++ netclient/config/config.go | 4 +++- netclient/main.go | 7 ++++--- netclient/ncutils/netclientutils.go | 6 ++++-- 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/netclient/cli_options/cmds.go b/netclient/cli_options/cmds.go index 26314e2b..1a556d83 100644 --- a/netclient/cli_options/cmds.go +++ b/netclient/cli_options/cmds.go @@ -3,11 +3,22 @@ package cli_options import ( "errors" + "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/netclient/command" "github.com/gravitl/netmaker/netclient/config" "github.com/urfave/cli/v2" ) +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 + } +} + // GetCommands - return commands that CLI uses func GetCommands(cliFlags []cli.Flag) []*cli.Command { return []*cli.Command{ @@ -16,6 +27,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command { Usage: "Join a Netmaker network.", Flags: cliFlags, Action: func(c *cli.Context) error { + parseVerbosity(c) cfg, pvtKey, err := config.GetCLIConfig(c) if err != nil { return err @@ -39,6 +51,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command { // the action, or code that will be executed when // we execute our `ns` command Action: func(c *cli.Context) error { + parseVerbosity(c) cfg, _, err := config.GetCLIConfig(c) if err != nil { return err @@ -54,6 +67,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command { // the action, or code that will be executed when // we execute our `ns` command Action: func(c *cli.Context) error { + parseVerbosity(c) cfg, _, err := config.GetCLIConfig(c) if err != nil { return err @@ -69,6 +83,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command { // the action, or code that will be executed when // we execute our `ns` command Action: func(c *cli.Context) error { + parseVerbosity(c) cfg, _, err := config.GetCLIConfig(c) if err != nil { return err @@ -84,6 +99,7 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command { // the action, or code that will be executed when // we execute our `ns` command Action: func(c *cli.Context) error { + parseVerbosity(c) err := command.Uninstall() return err }, @@ -93,6 +109,8 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command { Usage: "run netclient as daemon", Flags: cliFlags, Action: func(c *cli.Context) error { + // set max verbosity for daemon regardless + logger.Verbosity = 3 err := command.Daemon() return err }, diff --git a/netclient/cli_options/flags.go b/netclient/cli_options/flags.go index 5ac99ab9..081caf4c 100644 --- a/netclient/cli_options/flags.go +++ b/netclient/cli_options/flags.go @@ -204,5 +204,23 @@ func GetFlags(hostname string) []cli.Flag { Value: "no", 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.", + }, } } diff --git a/netclient/config/config.go b/netclient/config/config.go index 40ed3cf9..9c290b23 100644 --- a/netclient/config/config.go +++ b/netclient/config/config.go @@ -25,7 +25,6 @@ type ClientConfig struct { Daemon string `yaml:"daemon"` OperatingSystem string `yaml:"operatingsystem"` DebugOn bool `yaml:"debugon"` - } // ServerConfig - struct for dealing with the server information for a netclient @@ -168,6 +167,9 @@ func ReplaceWithBackup(network string) error { // GetCLIConfig - gets the cli flags as a config func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) { var cfg ClientConfig + if c.String("version") != "" { + return cfg, c.String("version"), nil + } if c.String("token") != "" { tokenbytes, err := base64.StdEncoding.DecodeString(c.String("token")) if err != nil { diff --git a/netclient/main.go b/netclient/main.go index 54bc3538..f4b92a8a 100644 --- a/netclient/main.go +++ b/netclient/main.go @@ -17,13 +17,14 @@ var version = "dev" func main() { 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 ncutils.SetVersion(version) - cliFlags := cli_options.GetFlags(ncutils.GetHostname()) 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() diff --git a/netclient/ncutils/netclientutils.go b/netclient/ncutils/netclientutils.go index f7815c58..c37198ac 100644 --- a/netclient/ncutils/netclientutils.go +++ b/netclient/ncutils/netclientutils.go @@ -27,8 +27,10 @@ import ( "google.golang.org/grpc/credentials" ) -// Version - version of the netclient -var Version = "dev" +var ( + // Version - version of the netclient + Version = "dev" +) // MAX_NAME_LENGTH - maximum node name length const MAX_NAME_LENGTH = 62 From fc5fc3d3819972013b77beb981f4d37c6b3c7e3e Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Thu, 24 Mar 2022 11:31:07 -0400 Subject: [PATCH 2/6] removed unneccesary check --- logger/util.go | 6 ++++++ netclient/config/config.go | 3 --- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/logger/util.go b/logger/util.go index aa3242b9..5dc42af1 100644 --- a/logger/util.go +++ b/logger/util.go @@ -6,6 +6,9 @@ import ( "strings" ) +// Verbosity - current logging verbosity of netclient +var Verbosity = 0 + // MakeString - makes a string using golang string builder func MakeString(delimeter string, message ...string) string { var builder strings.Builder @@ -19,6 +22,9 @@ func MakeString(delimeter string, message ...string) string { } func getVerbose() int32 { + if Verbosity >= 1 && Verbosity <= 3 { + return int32(Verbosity) + } level, err := strconv.Atoi(os.Getenv("VERBOSITY")) if err != nil || level < 0 { level = 0 diff --git a/netclient/config/config.go b/netclient/config/config.go index 9c290b23..350a8701 100644 --- a/netclient/config/config.go +++ b/netclient/config/config.go @@ -167,9 +167,6 @@ func ReplaceWithBackup(network string) error { // GetCLIConfig - gets the cli flags as a config func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) { var cfg ClientConfig - if c.String("version") != "" { - return cfg, c.String("version"), nil - } if c.String("token") != "" { tokenbytes, err := base64.StdEncoding.DecodeString(c.String("token")) if err != nil { From 4aea8c483224b3302cb843669bc209738bc00897 Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Thu, 24 Mar 2022 11:32:58 -0400 Subject: [PATCH 3/6] adjusted private func placement --- netclient/cli_options/cmds.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/netclient/cli_options/cmds.go b/netclient/cli_options/cmds.go index 1a556d83..d1da3572 100644 --- a/netclient/cli_options/cmds.go +++ b/netclient/cli_options/cmds.go @@ -9,16 +9,6 @@ import ( "github.com/urfave/cli/v2" ) -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 - } -} - // GetCommands - return commands that CLI uses func GetCommands(cliFlags []cli.Flag) []*cli.Command { return []*cli.Command{ @@ -117,3 +107,15 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command { }, } } + +// == 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 + } +} From f272ef1022a02d5e24241ee9b0239a3e56f191f1 Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Thu, 24 Mar 2022 11:34:22 -0400 Subject: [PATCH 4/6] adjusted comment --- logger/util.go | 2 +- netclient/ncutils/netclientutils.go | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/logger/util.go b/logger/util.go index 5dc42af1..8e4c1bef 100644 --- a/logger/util.go +++ b/logger/util.go @@ -6,7 +6,7 @@ import ( "strings" ) -// Verbosity - current logging verbosity of netclient +// Verbosity - current logging verbosity level (optionally set) var Verbosity = 0 // MakeString - makes a string using golang string builder diff --git a/netclient/ncutils/netclientutils.go b/netclient/ncutils/netclientutils.go index c37198ac..f7815c58 100644 --- a/netclient/ncutils/netclientutils.go +++ b/netclient/ncutils/netclientutils.go @@ -27,10 +27,8 @@ import ( "google.golang.org/grpc/credentials" ) -var ( - // Version - version of the netclient - Version = "dev" -) +// Version - version of the netclient +var Version = "dev" // MAX_NAME_LENGTH - maximum node name length const MAX_NAME_LENGTH = 62 From 00787dd853da4ffbf68edc243666c686ee3152c1 Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Thu, 24 Mar 2022 13:50:35 -0400 Subject: [PATCH 5/6] changed to lowercase --- netclient/cli_options/cmds.go | 13 +++++++++++++ netclient/cli_options/flags.go | 6 +++--- netclient/main.go | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/netclient/cli_options/cmds.go b/netclient/cli_options/cmds.go index d1da3572..849c5f24 100644 --- a/netclient/cli_options/cmds.go +++ b/netclient/cli_options/cmds.go @@ -2,6 +2,7 @@ package cli_options import ( "errors" + "fmt" "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/netclient/command" @@ -105,6 +106,16 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command { return err }, }, + { + Name: "verbosity", + Usage: "run netclient verb as daemon", + Flags: cliFlags, + Action: func(c *cli.Context) error { + // set max verbosity for daemon regardless + parseVerbosity(c) + return nil + }, + }, } } @@ -118,4 +129,6 @@ func parseVerbosity(c *cli.Context) { } else if c.Bool("VVV") { logger.Verbosity = 3 } + logger.Log(0, fmt.Sprintf("set verbosity to %d", logger.Verbosity)) + } diff --git a/netclient/cli_options/flags.go b/netclient/cli_options/flags.go index 081caf4c..317e014b 100644 --- a/netclient/cli_options/flags.go +++ b/netclient/cli_options/flags.go @@ -206,19 +206,19 @@ func GetFlags(hostname string) []cli.Flag { }, &cli.BoolFlag{ Name: "verbosity-level-1", - Aliases: []string{"V"}, + Aliases: []string{"v"}, Value: false, Usage: "Netclient Verbosity level 1.", }, &cli.BoolFlag{ Name: "verbosity-level-2", - Aliases: []string{"VV"}, + Aliases: []string{"vv"}, Value: false, Usage: "Netclient Verbosity level 2.", }, &cli.BoolFlag{ Name: "verbosity-level-3", - Aliases: []string{"VVV"}, + Aliases: []string{"vvv"}, Value: false, Usage: "Netclient Verbosity level 3.", }, diff --git a/netclient/main.go b/netclient/main.go index f4b92a8a..26b5427b 100644 --- a/netclient/main.go +++ b/netclient/main.go @@ -24,7 +24,7 @@ func main() { 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)." + app.UsageText = "netclient [global options] command [command options] [arguments...]. Adjust verbosity of given command with -v, -vv or -vvv (max)." setGarbageCollection() From 8ce93ce37451e6459c6aee7fb8bb40e18ffebc01 Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Thu, 24 Mar 2022 13:52:40 -0400 Subject: [PATCH 6/6] adjusted to lowercase --- netclient/cli_options/cmds.go | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/netclient/cli_options/cmds.go b/netclient/cli_options/cmds.go index 849c5f24..8ae39dca 100644 --- a/netclient/cli_options/cmds.go +++ b/netclient/cli_options/cmds.go @@ -2,7 +2,6 @@ package cli_options import ( "errors" - "fmt" "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/netclient/command" @@ -106,29 +105,17 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command { return err }, }, - { - Name: "verbosity", - Usage: "run netclient verb as daemon", - Flags: cliFlags, - Action: func(c *cli.Context) error { - // set max verbosity for daemon regardless - parseVerbosity(c) - return nil - }, - }, } } // == Private funcs == func parseVerbosity(c *cli.Context) { - if c.Bool("V") { + if c.Bool("v") { logger.Verbosity = 1 - } else if c.Bool("VV") { + } else if c.Bool("vv") { logger.Verbosity = 2 - } else if c.Bool("VVV") { + } else if c.Bool("vvv") { logger.Verbosity = 3 } - logger.Log(0, fmt.Sprintf("set verbosity to %d", logger.Verbosity)) - }