package cfg import ( "fmt" "os" "github.com/alecthomas/kong" ) type AppSettings struct { 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"` 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 convert grafana dashboards to git"), kong.UsageOnError(), ) validateFlags(ctx) settings := &AppSettings{ 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 } 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 } 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() for i := 0; i < len(messages); i++ { fmt.Println(messages[i]) } os.Exit(1) } }