Add configuration flags & env variables
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
0fb6e0b6e9
commit
52703cb28f
2 changed files with 51 additions and 26 deletions
49
cfg/cfg.go
49
cfg/cfg.go
|
@ -8,27 +8,48 @@ import (
|
|||
)
|
||||
|
||||
type AppSettings struct {
|
||||
GrafanaURL string
|
||||
Token string
|
||||
GrafanaURL string
|
||||
GrafanaToken string
|
||||
GitBranch string
|
||||
GitRepoURL string
|
||||
GitUser string
|
||||
GitEmail string
|
||||
GitPass string
|
||||
GPGKey string
|
||||
}
|
||||
|
||||
var cliStruct struct {
|
||||
GrafanaURL string `name:"grafana-url" env:"GRAFANA_URL" help:"Grafana URL to access the API"`
|
||||
AuthToken string `name:"grafana-auth-token" env:"GRAFANA_AUTH_TOKEN" help:"Grafana auth token to access the API"`
|
||||
GrafanaURL string `name:"grafana-url" env:"GRAFANA_URL" help:"Grafana URL to access the API"`
|
||||
GrafanaToken string `name:"grafana-auth-token" env:"GRAFANA_AUTH_TOKEN" help:"Grafana auth token to access the API"`
|
||||
GitBranch string `name:"git-branch" env:"GIT_BRANCH" help:"Git branch name" default:"${default_git_branch}"`
|
||||
GitRepoURL string `name:"git-repo-url" env:"GIT_REPO_URL" help:"Complete Git repository URL"`
|
||||
GitUser string `name:"git-user" env:"GIT_USER" help:"Git username"`
|
||||
GitEmail string `name:"git-email" env:"GIT_EMAIL" help:"Git email address"`
|
||||
GitPass string `name:"git-pass" env:"GIT_PASS" help:"Git password"`
|
||||
GPGKey string `name:"signing-key" env:"GIT_SIGNING_KEY" help:"GPG signing key"`
|
||||
}
|
||||
|
||||
func Parse() *AppSettings {
|
||||
ctx := kong.Parse(
|
||||
&cliStruct,
|
||||
kong.Vars{
|
||||
"default_git_branch": "main",
|
||||
},
|
||||
kong.Name("grafana-backuper"),
|
||||
kong.Description("🚀 CLI tool to backup grafana dashboards"),
|
||||
kong.Description("🚀 CLI tool to convert grafana dashboards to git"),
|
||||
kong.UsageOnError(),
|
||||
)
|
||||
|
||||
validateFlags(ctx)
|
||||
settings := &AppSettings{
|
||||
GrafanaURL: cliStruct.GrafanaURL,
|
||||
Token: cliStruct.AuthToken,
|
||||
GrafanaURL: cliStruct.GrafanaURL,
|
||||
GrafanaToken: cliStruct.GrafanaToken,
|
||||
GitBranch: cliStruct.GitBranch,
|
||||
GitRepoURL: cliStruct.GitRepoURL,
|
||||
GitUser: cliStruct.GitUser,
|
||||
GitEmail: cliStruct.GitEmail,
|
||||
GitPass: cliStruct.GitPass,
|
||||
GPGKey: cliStruct.GPGKey,
|
||||
}
|
||||
return settings
|
||||
}
|
||||
|
@ -40,10 +61,22 @@ func validateFlags(cliCtx *kong.Context) {
|
|||
messages = append(messages, "error: invalid grafana URL, must not be blank")
|
||||
flagsValid = false
|
||||
}
|
||||
if cliStruct.AuthToken == "" {
|
||||
if cliStruct.GrafanaToken == "" {
|
||||
messages = append(messages, "error: invalid auth token for grafana, must not be blank")
|
||||
flagsValid = false
|
||||
}
|
||||
if cliStruct.GitRepoURL == "" {
|
||||
messages = append(messages, "error: invalid repo url for git, must not be blank")
|
||||
flagsValid = false
|
||||
}
|
||||
if cliStruct.GitUser == "" {
|
||||
messages = append(messages, "error: invalid username for git, must not be blank")
|
||||
flagsValid = false
|
||||
}
|
||||
if cliStruct.GitPass == "" {
|
||||
messages = append(messages, "error: invalid password for git, must not be blank")
|
||||
flagsValid = false
|
||||
}
|
||||
if !flagsValid {
|
||||
cliCtx.PrintUsage(false)
|
||||
fmt.Println()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue