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

View file

@ -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")
}
}

View file

@ -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
}

View file

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