package cfg import ( "fmt" "os" "github.com/alecthomas/kong" ) type AppSettings struct { GrafanaURL string Token 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"` } func Parse() *AppSettings { ctx := kong.Parse( &cliStruct, kong.Name("grafana-backuper"), kong.Description("🚀 CLI tool to backup grafana dashboards"), kong.UsageOnError(), ) validateFlags(ctx) settings := &AppSettings{ GrafanaURL: cliStruct.GrafanaURL, Token: cliStruct.AuthToken, } 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.AuthToken == "" { messages = append(messages, "error: invalid auth token for grafana, 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) } }