style: make linter happy
This commit is contained in:
parent
886c9dffa8
commit
20a47d9abc
6 changed files with 34 additions and 6 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue