From 9b5bda50426a6791e67302f5c7c70e27462ae7db Mon Sep 17 00:00:00 2001 From: Adora Laura Kalb Date: Sun, 30 Jun 2024 08:55:07 +0200 Subject: [PATCH] enable different log levels --- cmd/certwarden-deploy/root.go | 13 +++++++++++++ internal/config/models.go | 3 +++ internal/logger/logger.go | 13 +++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/cmd/certwarden-deploy/root.go b/cmd/certwarden-deploy/root.go index febbb53..4eae9c9 100644 --- a/cmd/certwarden-deploy/root.go +++ b/cmd/certwarden-deploy/root.go @@ -8,6 +8,9 @@ import ( "os" "code.lila.network/adoralaura/certwarden-deploy/internal/cli" + "code.lila.network/adoralaura/certwarden-deploy/internal/config" + "code.lila.network/adoralaura/certwarden-deploy/internal/logger" + "github.com/spf13/cobra" ) var cfgFile string @@ -20,3 +23,13 @@ func Execute() { os.Exit(1) } } + +func init() { + cobra.OnInitialize(config.InitializeConfig, logger.InitializeLogger) + + cli.RootCmd.PersistentFlags().BoolVarP(&config.VerboseLogging, "verbose", "v", false, "Enable verbose logging") + cli.RootCmd.PersistentFlags().BoolVarP(&config.DryRun, "dry-run", "d", false, "Just show the would-be changes without changing the file system") + cli.RootCmd.PersistentFlags().BoolVarP(&config.QuietLogging, "quiet", "q", false, "Disable any logging (if both -q and -v are set, quiet wins)") + cli.RootCmd.PersistentFlags().StringVarP(&config.ConfigFile, "config", "c", "/etc/certwarden-deploy/config.yaml", "Path to config file (default is /etc/certwarden-deploy/config.yaml)") + +} diff --git a/internal/config/models.go b/internal/config/models.go index 42d5e29..c086e65 100644 --- a/internal/config/models.go +++ b/internal/config/models.go @@ -5,6 +5,9 @@ import "log/slog" var Config *ConfigFileData var ConfigFile *string var Logger *slog.Logger +var DryRun bool +var QuietLogging bool +var VerboseLogging bool type ConfigFileData struct { BaseURL string `yaml:"base_url"` diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 4cb074c..28cc7c8 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -3,13 +3,22 @@ package logger import ( "log/slog" "os" + + "code.lila.network/adoralaura/certwarden-deploy/internal/config" ) func InitializeLogger() { - // TODO: Different Log levels + logLevel := slog.LevelInfo + + if config.VerboseLogging { + logLevel = slog.LevelDebug + } + if config.QuietLogging { + logLevel = slog.LevelError + } opts := &slog.HandlerOptions{ - Level: slog.LevelInfo, + Level: logLevel, } handler := slog.NewTextHandler(os.Stdout, opts)