2024-02-15 01:12:57 +01:00
|
|
|
package cfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/alecthomas/kong"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppSettings struct {
|
2024-02-16 22:16:29 +01:00
|
|
|
GrafanaURL string
|
|
|
|
GrafanaToken string
|
|
|
|
GitBranch string
|
|
|
|
GitRepoURL string
|
|
|
|
GitUser string
|
|
|
|
GitEmail string
|
|
|
|
GitPass string
|
|
|
|
GPGKey string
|
2024-02-15 01:12:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var cliStruct struct {
|
2024-02-16 22:16:29 +01:00
|
|
|
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"`
|
2024-02-15 01:12:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Parse() *AppSettings {
|
|
|
|
ctx := kong.Parse(
|
|
|
|
&cliStruct,
|
2024-02-16 22:16:29 +01:00
|
|
|
kong.Vars{
|
|
|
|
"default_git_branch": "main",
|
|
|
|
},
|
2024-02-15 01:12:57 +01:00
|
|
|
kong.Name("grafana-backuper"),
|
2024-02-16 22:16:29 +01:00
|
|
|
kong.Description("🚀 CLI tool to convert grafana dashboards to git"),
|
2024-02-15 01:12:57 +01:00
|
|
|
kong.UsageOnError(),
|
|
|
|
)
|
|
|
|
|
|
|
|
validateFlags(ctx)
|
|
|
|
settings := &AppSettings{
|
2024-02-16 22:16:29 +01:00
|
|
|
GrafanaURL: cliStruct.GrafanaURL,
|
|
|
|
GrafanaToken: cliStruct.GrafanaToken,
|
|
|
|
GitBranch: cliStruct.GitBranch,
|
|
|
|
GitRepoURL: cliStruct.GitRepoURL,
|
|
|
|
GitUser: cliStruct.GitUser,
|
|
|
|
GitEmail: cliStruct.GitEmail,
|
|
|
|
GitPass: cliStruct.GitPass,
|
|
|
|
GPGKey: cliStruct.GPGKey,
|
2024-02-15 01:12:57 +01:00
|
|
|
}
|
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateFlags(cliCtx *kong.Context) {
|
|
|
|
var flagsValid = true
|
|
|
|
var messages = []string{}
|
|
|
|
if cliStruct.GrafanaURL == "" {
|
|
|
|
messages = append(messages, "error: invalid grafana URL, must not be blank")
|
|
|
|
flagsValid = false
|
|
|
|
}
|
2024-02-16 22:16:29 +01:00
|
|
|
if cliStruct.GrafanaToken == "" {
|
2024-02-15 01:12:57 +01:00
|
|
|
messages = append(messages, "error: invalid auth token for grafana, must not be blank")
|
|
|
|
flagsValid = false
|
|
|
|
}
|
2024-02-16 22:16:29 +01:00
|
|
|
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
|
|
|
|
}
|
2024-02-15 01:12:57 +01:00
|
|
|
if !flagsValid {
|
|
|
|
cliCtx.PrintUsage(false)
|
|
|
|
fmt.Println()
|
|
|
|
for i := 0; i < len(messages); i++ {
|
|
|
|
fmt.Println(messages[i])
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|