enable different log levels

This commit is contained in:
Adora Laura Kalb 2024-06-30 08:55:07 +02:00
parent a08df81967
commit 9b5bda5042
Signed by: adoralaura
SSH key fingerprint: SHA256:3XrkbR8ikAZJVtYfaUliX1MhmJYVAe/ocIb/MiDHBJ8
3 changed files with 27 additions and 2 deletions

View file

@ -8,6 +8,9 @@ import (
"os" "os"
"code.lila.network/adoralaura/certwarden-deploy/internal/cli" "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 var cfgFile string
@ -20,3 +23,13 @@ func Execute() {
os.Exit(1) 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)")
}

View file

@ -5,6 +5,9 @@ import "log/slog"
var Config *ConfigFileData var Config *ConfigFileData
var ConfigFile *string var ConfigFile *string
var Logger *slog.Logger var Logger *slog.Logger
var DryRun bool
var QuietLogging bool
var VerboseLogging bool
type ConfigFileData struct { type ConfigFileData struct {
BaseURL string `yaml:"base_url"` BaseURL string `yaml:"base_url"`

View file

@ -3,13 +3,22 @@ package logger
import ( import (
"log/slog" "log/slog"
"os" "os"
"code.lila.network/adoralaura/certwarden-deploy/internal/config"
) )
func InitializeLogger() { func InitializeLogger() {
// TODO: Different Log levels logLevel := slog.LevelInfo
if config.VerboseLogging {
logLevel = slog.LevelDebug
}
if config.QuietLogging {
logLevel = slog.LevelError
}
opts := &slog.HandlerOptions{ opts := &slog.HandlerOptions{
Level: slog.LevelInfo, Level: logLevel,
} }
handler := slog.NewTextHandler(os.Stdout, opts) handler := slog.NewTextHandler(os.Stdout, opts)