From 52703cb28fcc31ff15c41115db2078173ebcd055 Mon Sep 17 00:00:00 2001 From: Tom Neuber Date: Fri, 16 Feb 2024 22:16:29 +0100 Subject: [PATCH] Add configuration flags & env variables --- cfg/cfg.go | 49 +++++++++++++++++++++++++++++++++++++++++-------- main.go | 28 ++++++++++------------------ 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/cfg/cfg.go b/cfg/cfg.go index 88634d9..de9fee1 100644 --- a/cfg/cfg.go +++ b/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() diff --git a/main.go b/main.go index e0a9724..064c5ef 100644 --- a/main.go +++ b/main.go @@ -6,34 +6,26 @@ import ( "log" "slices" + "git.ar21.de/yolokube/grafana-backuper/cfg" "git.ar21.de/yolokube/grafana-backuper/pkg/git" "git.ar21.de/yolokube/grafana-backuper/pkg/grafana" ) -const ( - grafanaURL = "https://grafana.services.yolokube.de" - grafanaAPIKey = "" - gitBranch = "main" - gitRepoURL = "" - gitUsername = "" - gitEmail = "" - gitPassword = "" - privateGPGKeyFile = "" -) - func main() { + appSettings := cfg.Parse() + ctx := context.Background() - client, err := grafana.NewClient(grafanaURL, grafanaAPIKey, grafana.DefaultHTTPClient) + client, err := grafana.NewClient(appSettings.GrafanaURL, appSettings.GrafanaToken, grafana.DefaultHTTPClient) if err != nil { log.Fatalf("Error creating the grafana client: %v", err) } - gitdata := git.NewPayload(privateGPGKeyFile) - if err = gitdata.GetRepo(gitRepoURL, gitUsername, gitPassword); err != nil { + gitdata := git.NewPayload(appSettings.GPGKey) + if err = gitdata.GetRepo(appSettings.GitRepoURL, appSettings.GitUser, appSettings.GitPass); err != nil { log.Fatalf("Error cloning git repo: %v", err) } - gitdata.AddCommitter(gitUsername, gitEmail) + gitdata.AddCommitter(appSettings.GitUser, appSettings.GitEmail) dashboards, err := client.SearchDashboards(ctx, "", false) if err != nil { @@ -60,7 +52,7 @@ func main() { for _, version := range versions { gitdata.UpdateVersion(version) - if gitdata.IsVersionCommitted(gitBranch) { + if gitdata.IsVersionCommitted(appSettings.GitBranch) { fmt.Printf("%s/%s - %s: %s -> already committed\n", dashboardInfo.FolderTitle, dashboard.Title, version.CreatedBy, version.Message) continue } @@ -85,8 +77,8 @@ func main() { log.Fatalf("Error creating commit for dashboard %s version %d: %v", dashboard.Title, info.Version, err) } - if err = gitdata.PushToRemote(gitUsername, gitPassword); err != nil { - log.Fatalf("Error pushing to remote repo %s: %v", gitRepoURL, err) + if err = gitdata.PushToRemote(appSettings.GitUser, appSettings.GitPass); err != nil { + log.Fatalf("Error pushing to remote repo %s: %v", appSettings.GitRepoURL, err) } } }