diff --git a/cmd/jpictl/dump.go b/cmd/jpictl/dump.go index fb892ba..8a694f1 100644 --- a/cmd/jpictl/dump.go +++ b/cmd/jpictl/dump.go @@ -52,8 +52,9 @@ const encoding = YAMLEncoding // Command var dumpCmd = &cobra.Command{ - Use: "dump", - Short: "generates a text representation of the config", + Use: "dump", + Short: "generates a text representation of the config", + PreRun: setVerbosity, RunE: func(_ *cobra.Command, _ []string) error { var buf bytes.Buffer var enc Encoder diff --git a/cmd/jpictl/env.go b/cmd/jpictl/env.go index 4b21773..224d92d 100644 --- a/cmd/jpictl/env.go +++ b/cmd/jpictl/env.go @@ -8,8 +8,9 @@ import ( // Command var envCmd = &cobra.Command{ - Use: "env", - Short: "generates environment variables for shell scripts", + Use: "env", + Short: "generates environment variables for shell scripts", + PreRun: setVerbosity, RunE: func(_ *cobra.Command, _ []string) error { m, err := cfg.LoadZones(false) if err != nil { diff --git a/cmd/jpictl/gateway.go b/cmd/jpictl/gateway.go index 1004db3..41b869b 100644 --- a/cmd/jpictl/gateway.go +++ b/cmd/jpictl/gateway.go @@ -19,8 +19,9 @@ var gatewayCmd = &cobra.Command{ // gateway set var gatewaySetCmd = &cobra.Command{ - Use: "set", - Short: "gateway set sets machines as gateways", + Use: "set", + Short: "gateway set sets machines as gateways", + PreRun: setVerbosity, RunE: func(_ *cobra.Command, args []string) error { m, err := cfg.LoadZones(false) if err != nil { diff --git a/cmd/jpictl/log.go b/cmd/jpictl/log.go index bb5f65c..709f5b7 100644 --- a/cmd/jpictl/log.go +++ b/cmd/jpictl/log.go @@ -3,15 +3,9 @@ package main import ( "fmt" - "darvaza.org/sidecar/pkg/logger/zerolog" "darvaza.org/slog" ) -// TODO: make log level configurable via flags -var ( - log = zerolog.New(nil, slog.Debug) -) - // fatal is a convenience wrapper for slog.Logger.Fatal().Print() func fatal(err error, msg string, args ...any) { l := log.Fatal() diff --git a/cmd/jpictl/main.go b/cmd/jpictl/main.go index 9e9f6a8..14cf10c 100644 --- a/cmd/jpictl/main.go +++ b/cmd/jpictl/main.go @@ -2,6 +2,8 @@ package main import ( + "darvaza.org/sidecar/pkg/logger/zerolog" + "darvaza.org/slog" "github.com/spf13/cobra" ) @@ -10,13 +12,29 @@ const ( CmdName = "jpictl" ) -var rootCmd = &cobra.Command{ - Use: CmdName, - Short: "control tool for jpi.cloud", -} +var ( + log = zerolog.New(nil, slog.Error) + verbosity int + rootCmd = &cobra.Command{ + Use: CmdName, + Short: "control tool for jpi.cloud", + } +) func main() { if err := rootCmd.Execute(); err != nil { fatal(err, "") } } + +func init() { + rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v", "increase the verbosity level to Warn, Info or Debug") +} + +func setVerbosity(_ *cobra.Command, _ []string) { + desired := int8(slog.Error) + int8(verbosity) + if desired > 6 { + desired = 6 + } + log = log.WithLevel(slog.LogLevel(desired)) +} diff --git a/cmd/jpictl/write.go b/cmd/jpictl/write.go index acb79c2..be9117f 100644 --- a/cmd/jpictl/write.go +++ b/cmd/jpictl/write.go @@ -6,8 +6,9 @@ import ( // Command var writeCmd = &cobra.Command{ - Use: "write", - Short: "rewrites all config files", + Use: "write", + Short: "rewrites all config files", + PreRun: setVerbosity, RunE: func(_ *cobra.Command, _ []string) error { m, err := cfg.LoadZones(false) if err != nil {