-
Notifications
You must be signed in to change notification settings - Fork 3
/
version.go
48 lines (39 loc) · 1.2 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
47
48
// Copyright (c) 2019-2020, 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"
)
const pathVersion = "version"
// GetVersion gets version information from the Key Service. The context controls the lifetime of
// the request.
//
// If an non-200 HTTP status code is received, an error wrapping an HTTPError is returned.
func (c *Client) GetVersion(ctx context.Context) (string, error) {
ref := &url.URL{Path: pathVersion}
req, err := c.NewRequest(ctx, http.MethodGet, ref, nil)
if err != nil {
return "", fmt.Errorf("%w", err)
}
res, err := c.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
}