55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
|
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"
|
||
|
)
|
||
|
|
||
|
//nolint:dupl // This function may be the same as or similar to other cmd functions.
|
||
|
func NewRestoreCommand(c *config.Config) *cobra.Command {
|
||
|
restoreCmd := &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)
|
||
|
}
|
||
|
},
|
||
|
}
|
||
|
|
||
|
restoreCmd.PersistentFlags().BoolVar(&c.ForceCommits, "force", false, "Force dashboards / ignore existence check")
|
||
|
|
||
|
return restoreCmd
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
}
|