Skip to content

Commit 51fd3b1

Browse files
committed
add http client timeout
1 parent dabb942 commit 51fd3b1

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- fix #8 ignore lines in a patch that are being removed rather than added / changed
88
- adds `-version` option to display the current cli version
9+
- fix #18 add basic version check
910

1011
## 0.4.0 2018-04-25
1112

cmd/pre-commit/main.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"os/exec"
1313
"path/filepath"
14+
"time"
1415

1516
"github.com/ONSdigital/git-diff-check/diffcheck"
1617
)
@@ -21,8 +22,13 @@ const (
2122
rejected = 1
2223
)
2324

24-
// Repository defines the github repo where the source code is located
25-
const Repository = "ONSdigital/git-diff-check"
25+
const (
26+
// Repository defines the github repo where the source code is located
27+
Repository = "ONSdigital/git-diff-check"
28+
29+
// LatestVersion gives the api location of the most recent tag in github
30+
LatestVersion = "https://api.github.com/repos/" + Repository + "/releases/latest"
31+
)
2632

2733
var target = flag.String("p", "", "(optional) path to repository")
2834
var showVersion bool
@@ -127,30 +133,31 @@ type VersionResponse struct {
127133

128134
func versionCheck() bool {
129135

130-
// TODO
131-
// Set default client timeouts etc
136+
var netClient = &http.Client{
137+
Timeout: time.Second * 2,
138+
}
132139

133-
resp, err := http.Get("https://api.github.com/repos/" + Repository + "/releases/latest")
140+
resp, err := netClient.Get(LatestVersion)
134141
if err != nil {
135-
fmt.Println(errors.New("Failed to check for new versions" + err.Error()))
142+
fmt.Println("Failed to check for new versions" + err.Error())
136143
return false
137144
}
138145

139146
body, err := ioutil.ReadAll(resp.Body)
140147
if err != nil {
141-
fmt.Println(errors.New("Failed to read response from version check" + err.Error()))
148+
fmt.Println("Failed to check for new versions" + err.Error())
142149
return false
143150
}
144151

145152
var v VersionResponse
146153
err = json.Unmarshal(body, &v)
147154
if err != nil {
148-
fmt.Println(errors.New("Failed to read response from version check" + err.Error()))
155+
fmt.Println("Failed to check for new versions" + err.Error())
149156
return false
150157
}
151158

152159
if len(v.TagName) == 0 {
153-
fmt.Println(errors.New("Failed to parse version from github response"))
160+
fmt.Println("Failed to parse version from github response")
154161
return false
155162
}
156163

0 commit comments

Comments
 (0)