feat(backup): use logrus for output

This commit is contained in:
Tom Neuber 2024-07-30 21:20:18 +02:00
parent 0c05b9ee2c
commit 0c093bb5c9
Signed by: tom
GPG key ID: F17EFE4272D89FF6
5 changed files with 23 additions and 18 deletions

1
go.mod
View file

@ -34,6 +34,7 @@ require (
github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.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/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/skeema/knownhosts v1.2.2 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect github.com/spf13/afero v1.11.0 // indirect

3
go.sum
View file

@ -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 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= 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.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 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A=
github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= 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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 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.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.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.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=

View file

@ -3,13 +3,13 @@ package cmd
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"slices" "slices"
"git.ar21.de/yolokube/grafana-backuper/internal/config" "git.ar21.de/yolokube/grafana-backuper/internal/config"
"git.ar21.de/yolokube/grafana-backuper/internal/helper" "git.ar21.de/yolokube/grafana-backuper/internal/helper"
"git.ar21.de/yolokube/grafana-backuper/pkg/git" "git.ar21.de/yolokube/grafana-backuper/pkg/git"
"git.ar21.de/yolokube/grafana-backuper/pkg/grafana" "git.ar21.de/yolokube/grafana-backuper/pkg/grafana"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -18,11 +18,15 @@ func NewBackupCommand(c *config.Config) *cobra.Command {
Use: "backup", Use: "backup",
Short: "Back up the dashboards from grafana to a git repository.", Short: "Back up the dashboards from grafana to a git repository.",
Long: "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 { Run: func(cmd *cobra.Command, _ []string) {
return backup(cmd.Context(), c) if err := backup(cmd.Context(), c); err != nil {
log.WithContext(cmd.Context()).WithError(err).Fatal()
}
}, },
PreRunE: func(_ *cobra.Command, _ []string) error { PreRun: func(cmd *cobra.Command, _ []string) {
return c.Validate() 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, uncommitedVersion,
) )
if errors.Is(err, helper.ErrAlreadyCommited) { 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 continue
} else if err != nil { } else if err != nil {
return err return err
} }
uncommitedVersion = true uncommitedVersion = true
fmt.Fprintln(c.Output, commitMsg) log.WithContext(ctx).WithField("commit-msg", commitMsg).Info("commit created")
} }
} }

View file

@ -7,6 +7,7 @@ import (
"git.ar21.de/yolokube/grafana-backuper/internal/config" "git.ar21.de/yolokube/grafana-backuper/internal/config"
"git.ar21.de/yolokube/grafana-backuper/internal/version" "git.ar21.de/yolokube/grafana-backuper/internal/version"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -20,20 +21,18 @@ func NewRootCommand(c *config.Config) *cobra.Command {
Short: "Grafana Backuper CLI", Short: "Grafana Backuper CLI",
Long: "A command-line tool to back up and restore Grafana dashboards", Long: "A command-line tool to back up and restore Grafana dashboards",
Version: version.Version(), Version: version.Version(),
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { PersistentPreRun: func(cmd *cobra.Command, _ []string) {
initializeConfig(cmd) initializeConfig(cmd)
c.Output = os.Stdout 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) cmd.SetOut(c.Output)
return nil
if c.Debug {
log.SetLevel(log.DebugLevel)
}
}, },
SilenceUsage: true, 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.GitRepo, "git-repo", "", "Complete Git repository URL")
rootCmd.PersistentFlags().StringVar(&c.GitUser, "git-user", "", "Git user name") rootCmd.PersistentFlags().StringVar(&c.GitUser, "git-user", "", "Git user name")
rootCmd.PersistentFlags().StringVar(&c.GitPass, "git-pass", "", "Git user password") rootCmd.PersistentFlags().StringVar(&c.GitPass, "git-pass", "", "Git user password")
rootCmd.PersistentFlags().BoolVarP(&c.Quiet, "quiet", "q", false, "Mute console output")
return rootCmd return rootCmd
} }

View file

@ -16,7 +16,6 @@ type Config struct {
GitUser string GitUser string
GitPass string GitPass string
GPGKey string GPGKey string
Quiet bool
Output io.Writer Output io.Writer
} }