From 0c093bb5c9302df937a91b26495e3078801857da Mon Sep 17 00:00:00 2001 From: Tom Neuber Date: Tue, 30 Jul 2024 21:20:18 +0200 Subject: [PATCH] feat(backup): use logrus for output --- go.mod | 1 + go.sum | 3 +++ internal/cmd/backup.go | 18 +++++++++++------- internal/cmd/root.go | 18 ++++++++---------- internal/config/config.go | 1 - 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 6521940..ccfac3e 100644 --- a/go.mod +++ b/go.mod @@ -34,6 +34,7 @@ require ( github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect github.com/skeema/knownhosts v1.2.2 // indirect github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect diff --git a/go.sum b/go.sum index fdef2d5..a0ac07a 100644 --- a/go.sum +++ b/go.sum @@ -94,6 +94,8 @@ github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWR github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A= github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= @@ -115,6 +117,7 @@ github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= diff --git a/internal/cmd/backup.go b/internal/cmd/backup.go index dbb545e..03a8196 100644 --- a/internal/cmd/backup.go +++ b/internal/cmd/backup.go @@ -3,13 +3,13 @@ package cmd import ( "context" "errors" - "fmt" "slices" "git.ar21.de/yolokube/grafana-backuper/internal/config" "git.ar21.de/yolokube/grafana-backuper/internal/helper" "git.ar21.de/yolokube/grafana-backuper/pkg/git" "git.ar21.de/yolokube/grafana-backuper/pkg/grafana" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -18,11 +18,15 @@ func NewBackupCommand(c *config.Config) *cobra.Command { Use: "backup", Short: "Back up the dashboards from grafana to a git repository.", Long: "Back up the dashboards from grafana to a git repository.", - RunE: func(cmd *cobra.Command, _ []string) error { - return backup(cmd.Context(), c) + Run: func(cmd *cobra.Command, _ []string) { + if err := backup(cmd.Context(), c); err != nil { + log.WithContext(cmd.Context()).WithError(err).Fatal() + } }, - PreRunE: func(_ *cobra.Command, _ []string) error { - return c.Validate() + PreRun: func(cmd *cobra.Command, _ []string) { + if err := c.Validate(); err != nil { + log.WithContext(cmd.Context()).WithError(err).Fatal("checking flags & environment variables") + } }, } @@ -91,14 +95,14 @@ func backup(ctx context.Context, c *config.Config) error { uncommitedVersion, ) if errors.Is(err, helper.ErrAlreadyCommited) { - fmt.Fprintf(c.Output, "%s -> %v\n", commitMsg, err) + log.WithContext(ctx).WithField("commit-msg", commitMsg).Info("already committed") continue } else if err != nil { return err } uncommitedVersion = true - fmt.Fprintln(c.Output, commitMsg) + log.WithContext(ctx).WithField("commit-msg", commitMsg).Info("commit created") } } diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 43bf4d8..30fb913 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -7,6 +7,7 @@ import ( "git.ar21.de/yolokube/grafana-backuper/internal/config" "git.ar21.de/yolokube/grafana-backuper/internal/version" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -20,20 +21,18 @@ func NewRootCommand(c *config.Config) *cobra.Command { Short: "Grafana Backuper CLI", Long: "A command-line tool to back up and restore Grafana dashboards", Version: version.Version(), - PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { + PersistentPreRun: func(cmd *cobra.Command, _ []string) { initializeConfig(cmd) c.Output = os.Stdout - if c.Quiet { - var err error - c.Output, err = os.Open(os.DevNull) - if err != nil { - return err - } - } + log.SetOutput(c.Output) + log.SetLevel(log.InfoLevel) cmd.SetOut(c.Output) - return nil + + if c.Debug { + log.SetLevel(log.DebugLevel) + } }, SilenceUsage: true, } @@ -47,7 +46,6 @@ func NewRootCommand(c *config.Config) *cobra.Command { rootCmd.PersistentFlags().StringVar(&c.GitRepo, "git-repo", "", "Complete Git repository URL") rootCmd.PersistentFlags().StringVar(&c.GitUser, "git-user", "", "Git user name") rootCmd.PersistentFlags().StringVar(&c.GitPass, "git-pass", "", "Git user password") - rootCmd.PersistentFlags().BoolVarP(&c.Quiet, "quiet", "q", false, "Mute console output") return rootCmd } diff --git a/internal/config/config.go b/internal/config/config.go index c86a1ac..cc6ac4e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -16,7 +16,6 @@ type Config struct { GitUser string GitPass string GPGKey string - Quiet bool Output io.Writer }