2024-07-27 18:56:09 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-19 14:13:16 +02:00
|
|
|
"os"
|
2024-07-27 18:56:09 +02:00
|
|
|
|
|
|
|
"git.ar21.de/yolokube/grafana-backuper/internal/config"
|
2024-08-19 14:13:16 +02:00
|
|
|
"git.ar21.de/yolokube/grafana-backuper/pkg/grafanabackuper"
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-07-27 18:56:09 +02:00
|
|
|
"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 {
|
2024-08-19 14:13:16 +02:00
|
|
|
log.Fatal().Err(err).Send()
|
2024-07-30 21:20:18 +02:00
|
|
|
}
|
2024-07-27 18:56:09 +02:00
|
|
|
},
|
2024-07-30 21:20:18 +02:00
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
2024-08-19 14:13:16 +02:00
|
|
|
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
|
|
|
}
|
2024-07-27 18:56:09 +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
|
|
|
|
}
|
|
|
|
|
2024-08-19 14:13:16 +02:00
|
|
|
func backup(ctx context.Context, cfg *config.Config) error {
|
|
|
|
client := grafanabackuper.NewClient(
|
|
|
|
grafanabackuper.WithZerologLogger(cfg.Logger),
|
2024-07-27 18:56:09 +02:00
|
|
|
)
|
|
|
|
|
2024-08-19 14:13:16 +02:00
|
|
|
if err := client.GetSigner(cfg); err != nil {
|
2024-07-27 18:56:09 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-19 14:13:16 +02:00
|
|
|
if err := client.Prepare(ctx, cfg); err != nil {
|
2024-07-27 18:56:09 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-19 14:13:16 +02:00
|
|
|
return client.Backup.Start(ctx, cfg)
|
2024-07-27 18:56:09 +02:00
|
|
|
}
|