From 20a47d9abcaf0994b5b3118fd7cce7c0c8e2c8b5 Mon Sep 17 00:00:00 2001 From: Tom Neuber Date: Wed, 19 Jun 2024 22:07:51 +0200 Subject: [PATCH] style: make linter happy --- cfg/cfg.go | 18 +++++++++++++----- main.go | 1 + pkg/git/git.go | 1 + pkg/grafana/dashboard.go | 8 ++++++++ pkg/grafana/requests.go | 9 ++++++++- pkg/grafana/search.go | 3 +++ 6 files changed, 34 insertions(+), 6 deletions(-) diff --git a/cfg/cfg.go b/cfg/cfg.go index b79bdc3..dac8eb7 100644 --- a/cfg/cfg.go +++ b/cfg/cfg.go @@ -43,7 +43,7 @@ func Parse() *AppSettings { ) validateFlags(ctx) - settings := &AppSettings{ + return &AppSettings{ Force: cliStruct.Force, GrafanaURL: cliStruct.GrafanaURL, GrafanaToken: cliStruct.GrafanaToken, @@ -54,34 +54,42 @@ func Parse() *AppSettings { GitPass: cliStruct.GitPass, GPGKey: cliStruct.GPGKey, } - return settings } func validateFlags(cliCtx *kong.Context) { - var flagsValid = true - var messages = []string{} + flagsValid := true + 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) + if err := cliCtx.PrintUsage(false); err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } fmt.Println() for i := 0; i < len(messages); i++ { fmt.Println(messages[i]) diff --git a/main.go b/main.go index 72162df..fd8c1bf 100644 --- a/main.go +++ b/main.go @@ -73,6 +73,7 @@ func main() { gitdata.UpdateContent([]byte(output)) gitdata.UpdateCommitter() + if err = gitdata.CreateCommit(); err != nil { log.Fatalf("Error creating commit for dashboard %s version %d: %v", dashboard.Title, info.Version, err) } diff --git a/pkg/git/git.go b/pkg/git/git.go index d696f55..b2b5df1 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -110,6 +110,7 @@ func (p *Payload) GetRepo(repoURL, user, password string) (err error) { func (p *Payload) IsVersionCommitted(branch string) bool { refName := plumbing.NewBranchReferenceName(branch) + ref, err := p.Repository.Reference(refName, false) if err != nil { return false diff --git a/pkg/grafana/dashboard.go b/pkg/grafana/dashboard.go index d501ad7..8b736d1 100644 --- a/pkg/grafana/dashboard.go +++ b/pkg/grafana/dashboard.go @@ -49,11 +49,14 @@ func (c *Client) getRawDashboardByUID(ctx context.Context, path string) ([]byte, if code != 200 { return raw, BoardProperties{}, fmt.Errorf("HTTP error %d: returns %s", code, raw) } + var result struct { Meta BoardProperties `json:"meta"` } + dec := json.NewDecoder(bytes.NewReader(raw)) dec.UseNumber() + if err := dec.Decode(&result); err != nil { return raw, BoardProperties{}, fmt.Errorf("failed unmarshalling dashboard from path %s: %v", path, err) } @@ -62,6 +65,7 @@ func (c *Client) getRawDashboardByUID(ctx context.Context, path string) ([]byte, func (c *Client) getRawDashboardFromVersion(ctx context.Context, path string) ([]byte, DashboardVersion, error) { var versionInfo DashboardVersion + raw, code, err := c.get(ctx, fmt.Sprintf("api/dashboards/%s", path), nil) if err != nil { return nil, versionInfo, err @@ -69,8 +73,10 @@ func (c *Client) getRawDashboardFromVersion(ctx context.Context, path string) ([ if code != 200 { return raw, versionInfo, fmt.Errorf("HTTP error %d: returns %s", code, raw) } + dec := json.NewDecoder(bytes.NewReader(raw)) dec.UseNumber() + if err := dec.Decode(&versionInfo); err != nil { return raw, versionInfo, fmt.Errorf("failed unmarshalling dashboard from path %s: %v", path, err) } @@ -80,6 +86,7 @@ func (c *Client) getRawDashboardFromVersion(ctx context.Context, path string) ([ func queryParams(params ...QueryParam) url.Values { u := url.URL{} q := u.Query() + for _, p := range params { p(&q) } @@ -99,6 +106,7 @@ func (c *Client) GetDashboardVersionsByDashboardUID(ctx context.Context, uid str if code != 200 { return nil, fmt.Errorf("HTTP error %d: returns %s", code, raw) } + var versions []DashboardVersion err = json.Unmarshal(raw, &versions) diff --git a/pkg/grafana/requests.go b/pkg/grafana/requests.go index d9f9c42..176060d 100644 --- a/pkg/grafana/requests.go +++ b/pkg/grafana/requests.go @@ -20,13 +20,14 @@ type Client struct { } func NewClient(apiURL, authString string, client *http.Client) (*Client, error) { - basicAuth := strings.Contains(authString, ":") baseURL, err := url.Parse(apiURL) if err != nil { return nil, err } var key string + basicAuth := strings.Contains(authString, ":") + if len(authString) > 0 { if !basicAuth { key = fmt.Sprintf("Bearer %s", authString) @@ -47,24 +48,30 @@ func NewClient(apiURL, authString string, client *http.Client) (*Client, error) func (c *Client) doRequest(ctx context.Context, method, query string, params url.Values, buf io.Reader) ([]byte, int, error) { u, _ := url.Parse(c.baseURL) u.Path = path.Join(u.Path, query) + if params != nil { u.RawQuery = params.Encode() } + req, err := http.NewRequest(method, u.String(), buf) if err != nil { return nil, 0, err } + req = req.WithContext(ctx) if !c.basicAuth && len(c.key) > 0 { req.Header.Set("Authorization", c.key) } + req.Header.Set("Accept", "application/json") req.Header.Set("Content-Type", "application/json") req.Header.Set("User-Agent", "grafana-backuper") + resp, err := c.client.Do(req) if err != nil { return nil, 0, err } + data, err := io.ReadAll(resp.Body) resp.Body.Close() return data, resp.StatusCode, err diff --git a/pkg/grafana/search.go b/pkg/grafana/search.go index 94dd1ea..7303a2a 100644 --- a/pkg/grafana/search.go +++ b/pkg/grafana/search.go @@ -47,15 +47,18 @@ func (c *Client) Search(ctx context.Context, params ...SearchParam) ([]FoundBoar ) u := url.URL{} q := u.Query() + for _, p := range params { p(&q) } + if raw, code, err = c.get(ctx, "api/search", q); err != nil { return nil, err } if code != 200 { return nil, fmt.Errorf("HTTP error %d: returns %s", code, raw) } + err = json.Unmarshal(raw, &boards) return boards, err }