grafana-backuper/internal/cmd/backup.go
Tom Neuber 579f2a5df7
All checks were successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
feat(cmd): add sequence flag to execute multiple functions
2024-12-07 01:38:24 +01:00

58 lines
1.4 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 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.",
Run: func(cmd *cobra.Command, _ []string) {
if err := backup(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 git commits / ignore existence check")
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)
}