grafana-backuper/internal/cmd/restore.go

54 lines
1.2 KiB
Go
Raw 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 NewRestoreCommand(c *config.Config) *cobra.Command {
backupCmd := &cobra.Command{
Use: "restore",
Short: "Restore the dashboards from a git repository to grafana.",
Long: "Restore the dashboards from a git repository to grafana.",
Run: func(cmd *cobra.Command, _ []string) {
if err := restore(cmd.Context(), c); err != nil {
log.Fatal().Err(err).Send()
}
},
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)
}
},
}
backupCmd.PersistentFlags().BoolVar(&c.ForceCommits, "force", false, "Force dashboards / ignore existence check")
return backupCmd
}
func restore(ctx context.Context, cfg *config.Config) error {
client := grafanabackuper.NewClient(
grafanabackuper.WithZerologLogger(cfg.Logger),
)
if err := client.Prepare(ctx, cfg); err != nil {
return err
}
return client.Restore.Start(ctx, cfg)
}