grafana-backuper/pkg/grafanabackuper/client.go

85 lines
1.8 KiB
Go
Raw Normal View History

package grafanabackuper
import (
"context"
"git.ar21.de/yolokube/grafana-backuper/internal/config"
"git.ar21.de/yolokube/grafana-backuper/pkg/git"
"git.ar21.de/yolokube/grafana-backuper/pkg/grafana"
"github.com/rs/zerolog"
)
type ClientOption func(*Client)
func WithZerologLogger(logger zerolog.Logger) ClientOption {
return func(c *Client) {
c.logger = logger
}
}
type Client struct {
git *git.Project
grafana *grafana.Client
logger zerolog.Logger
signer *git.SignKey
Backup BackupClient
}
func NewClient(options ...ClientOption) *Client {
client := &Client{}
for _, option := range options {
option(client)
}
client.Backup = BackupClient{client: client}
return client
}
func (c *Client) Prepare(ctx context.Context, cfg *config.Config) error {
c.logger.Debug().Msg("Creating new Grafana Client")
c.grafana = grafana.NewClient(
cfg.GrafanaURL,
grafana.WithToken(cfg.GrafanaToken),
)
c.logger.Debug().Msg("Created new Grafana Client successfully")
c.git = git.NewProject(
cfg.GitRepo,
git.WithBasicAuth(cfg.GitUser, cfg.GitPass),
git.WithBranch(cfg.GitBranch),
git.WithOutputWriter(cfg.Output),
)
c.logger.Debug().Msg("Cloning git project")
if err := c.git.Clone(ctx); err != nil {
return err
}
c.logger.Debug().Msg("Checking out git project")
if err := c.git.Checkout(); err != nil {
return err
}
c.logger.Debug().Msg("Pulling git project content")
if err := c.git.Pull(ctx); err != nil {
return err
}
c.logger.Debug().Msg("Loading git logs")
return c.git.LoadLogs()
}
func (c *Client) GetSigner(cfg *config.Config) error {
if cfg.GPGKey == "" {
c.logger.Debug().Msg("GPG key path parameter is empty")
return nil
}
c.signer = &git.SignKey{KeyFile: cfg.GPGKey}
c.logger.Debug().Msg("Reading GPG key file")
return c.signer.ReadKeyFile()
}