refactor(grafanabackuper): add restore function

This commit is contained in:
Tom Neuber 2024-08-19 20:12:28 +02:00
parent 83a15b5a87
commit 1f0d76ba9e
Signed by: tom
GPG key ID: F17EFE4272D89FF6
9 changed files with 214 additions and 8 deletions

View file

@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"
)
//nolint:dupl // This function may be the same as or similar to other cmd functions.
func NewBackupCommand(c *config.Config) *cobra.Command {
backupCmd := &cobra.Command{
Use: "backup",

54
internal/cmd/restore.go Normal file
View file

@ -0,0 +1,54 @@
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)
}