-
Notifications
You must be signed in to change notification settings - Fork 8
/
version.go
46 lines (38 loc) · 1.09 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.
package client
import (
"context"
"fmt"
"net/http"
"net/url"
jsonresp "github.com/sylabs/json-resp"
)
// GetVersion gets version information from the build service. The context controls the lifetime of
// the request.
func (c *Client) GetVersion(ctx context.Context) (string, error) {
ref := &url.URL{
Path: "version",
}
req, err := c.newRequest(ctx, http.MethodGet, ref, nil)
if err != nil {
return "", fmt.Errorf("%w", err)
}
res, err := c.httpClient.Do(req)
if err != nil {
return "", fmt.Errorf("%w", err)
}
defer res.Body.Close()
if res.StatusCode/100 != 2 { // non-2xx status code
return "", fmt.Errorf("%w", errorFromResponse(res))
}
vi := struct {
Version string `json:"version"`
}{}
if err := jsonresp.ReadResponse(res.Body, &vi); err != nil {
return "", fmt.Errorf("%w", err)
}
return vi.Version, nil
}