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