From 0c5429a681d35b374fa88b3be9ac52761b5fc85c Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sat, 28 Oct 2023 21:08:07 +0000 Subject: [PATCH] jpictl: move verbosity handling to the log module Signed-off-by: Alejandro Mery --- cmd/jpictl/log.go | 21 +++++++++++++++++++++ cmd/jpictl/main.go | 19 +------------------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/cmd/jpictl/log.go b/cmd/jpictl/log.go index 709f5b7..7f31282 100644 --- a/cmd/jpictl/log.go +++ b/cmd/jpictl/log.go @@ -3,9 +3,13 @@ package main import ( "fmt" + "darvaza.org/sidecar/pkg/logger/zerolog" "darvaza.org/slog" + "github.com/spf13/cobra" ) +var log = zerolog.New(nil, slog.Error) + // fatal is a convenience wrapper for slog.Logger.Fatal().Print() func fatal(err error, msg string, args ...any) { l := log.Fatal() @@ -19,3 +23,20 @@ func fatal(err error, msg string, args ...any) { panic("unreachable") } + +var verbosity int + +// setVerbosity replaces the global logger using the +// verbosity level specified via -v flags +func setVerbosity(_ *cobra.Command, _ []string) { + desired := int8(slog.Error) + int8(verbosity) + if desired > 6 { + desired = 6 + } + log = zerolog.New(nil, slog.LogLevel(desired)) +} + +func init() { + rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v", + "increase the verbosity level to Warn, Info or Debug") +} diff --git a/cmd/jpictl/main.go b/cmd/jpictl/main.go index 1cb84a6..5199ca6 100644 --- a/cmd/jpictl/main.go +++ b/cmd/jpictl/main.go @@ -2,8 +2,6 @@ package main import ( - "darvaza.org/sidecar/pkg/logger/zerolog" - "darvaza.org/slog" "github.com/spf13/cobra" ) @@ -13,9 +11,7 @@ const ( ) var ( - log = zerolog.New(nil, slog.Error) - verbosity int - rootCmd = &cobra.Command{ + rootCmd = &cobra.Command{ Use: CmdName, Short: "control tool for jpi.cloud", } @@ -26,16 +22,3 @@ func main() { 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 = zerolog.New(nil, slog.LogLevel(desired)) -}