grafana-backuper/internal/cmd/backup.go

60 lines
1.5 KiB
Go
Raw Permalink Normal View History

package cmd
import (
"context"
"os"
"git.ar21.de/yolokube/grafana-backuper/internal/config"
"git.ar21.de/yolokube/grafana-backuper/pkg/grafanabackuper"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
func NewBackupCommand(c *config.Config) *cobra.Command {
backupCmd := &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.",
2024-07-30 21:20:18 +02:00
Run: func(cmd *cobra.Command, _ []string) {
if err := backup(cmd.Context(), c); err != nil {
log.Fatal().Err(err).Send()
2024-07-30 21:20:18 +02:00
}
},
2024-07-30 21:20:18 +02:00
PreRun: func(cmd *cobra.Command, _ []string) {
errs := c.Validate()
for _, err := range errs {
log.Error().Err(err).Send()
}
if len(errs) > 0 {
if err := cmd.Help(); err != nil {
log.Error().Err(err).Send()
}
os.Exit(1)
2024-07-30 21:20:18 +02:00
}
},
}
backupCmd.PersistentFlags().BoolVar(&c.ForceCommits, "force", false, "Force git commits / ignore existence check")
backupCmd.PersistentFlags().StringVar(&c.GitEmail, "git-email", "", "Git email address")
backupCmd.PersistentFlags().StringVar(&c.GPGKey, "signing-key", "", "Path to the GPG signing key")
return backupCmd
}
func backup(ctx context.Context, cfg *config.Config) error {
client := grafanabackuper.NewClient(
grafanabackuper.WithZerologLogger(cfg.Logger),
)
if err := client.GetSigner(cfg); err != nil {
return err
}
if err := client.Prepare(ctx, cfg); err != nil {
return err
}
return client.Backup.Start(ctx, cfg)
}