refactor(cmd): add backup command to cli

This commit is contained in:
Tom Neuber 2024-07-27 18:56:09 +02:00
parent 577f821b9d
commit 62b316b5ef
Signed by: tom
GPG key ID: F17EFE4272D89FF6
8 changed files with 357 additions and 193 deletions

View file

@ -1,36 +1,52 @@
package config
import (
"flag"
"os"
"github.com/peterbourgon/ff"
"errors"
"io"
)
const envVarPrefix = "GB"
type Config struct {
Debug bool
ForceCommits bool
GrafanaURL string
GrafanaToken string
GitBranch string
GitEmail string
GitRepo string
GitUser string
GitEmail string
GitPass string
GPGKey string
Quiet bool
Output io.Writer
}
func (c *Config) RegisterFlags(fs *flag.FlagSet) error {
fs.BoolVar(&c.ForceCommits, "force-commits", false, "Force git commits / ignore existence check")
fs.StringVar(&c.GrafanaURL, "grafana-url", "", "Grafana URL to access the API")
fs.StringVar(&c.GrafanaToken, "grafana-token", "", "Grafana auth token to access the API")
fs.StringVar(&c.GitBranch, "git-branch", "main", "Git branch name")
fs.StringVar(&c.GitRepo, "git-repo", "", "Complete Git repository URL")
fs.StringVar(&c.GitUser, "git-user", "", "Git username")
fs.StringVar(&c.GitEmail, "git-email", "", "Git email address")
fs.StringVar(&c.GitPass, "git-pass", "", "Git user password")
fs.StringVar(&c.GPGKey, "signing-key", "", "Path to the GPG signing key")
func (c *Config) Validate() error {
var err error
return ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix(envVarPrefix))
if c.GrafanaURL == "" {
err = errors.Join(err, errors.New("invalid grafana URL, must not be blank"))
}
if c.GrafanaToken == "" {
err = errors.Join(err, errors.New("invalid auth token for grafana, must not be blank"))
}
if c.GitRepo == "" {
err = errors.Join(err, errors.New("invalid repo url for git, must not be blank"))
}
if c.GitUser == "" {
err = errors.Join(err, errors.New("invalid username for git, must not be blank"))
}
if c.GitPass == "" {
err = errors.Join(err, errors.New("invalid password for git, must not be blank"))
}
if c.GitBranch == "" {
err = errors.Join(err, errors.New("invalid branch name for git, must not be blank"))
}
return err
}