Skip to content

Commit dbd7412

Browse files
committed
feat(version): use context
1 parent b695b95 commit dbd7412

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

version/build.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package version
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"net/http"
@@ -31,6 +32,11 @@ type Revision struct {
3132

3233
// GetBuild gets build information.
3334
func GetBuild() (*Build, error) {
35+
return GetBuildWithContext(context.Background())
36+
}
37+
38+
// GetBuildWithContext gets build information.
39+
func GetBuildWithContext(ctx context.Context) (*Build, error) {
3440
dlURL, err := url.Parse(baseBuildURL)
3541
if err != nil {
3642
return nil, err
@@ -40,7 +46,12 @@ func GetBuild() (*Build, error) {
4046
query.Set("mode", "json")
4147
dlURL.RawQuery = query.Encode()
4248

43-
resp, err := http.Get(dlURL.String())
49+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, dlURL.String(), nil)
50+
if err != nil {
51+
return nil, err
52+
}
53+
54+
resp, err := http.DefaultClient.Do(req)
4455
if err != nil {
4556
return nil, err
4657
}

version/release.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package version
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"net/http"
@@ -29,6 +30,11 @@ type File struct {
2930

3031
// GetReleases gets build information.
3132
func GetReleases(all bool) ([]Release, error) {
33+
return GetReleasesWithContext(context.Background(), all)
34+
}
35+
36+
// GetReleasesWithContext gets build information.
37+
func GetReleasesWithContext(ctx context.Context, all bool) ([]Release, error) {
3238
dlURL, err := url.Parse(baseDLURL)
3339
if err != nil {
3440
return nil, err
@@ -43,7 +49,12 @@ func GetReleases(all bool) ([]Release, error) {
4349

4450
dlURL.RawQuery = query.Encode()
4551

46-
resp, err := http.Get(dlURL.String())
52+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, dlURL.String(), nil)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
resp, err := http.DefaultClient.Do(req)
4758
if err != nil {
4859
return nil, err
4960
}

0 commit comments

Comments
 (0)