@@ -12,6 +12,7 @@ import (
12
12
"archive/zip"
13
13
"compress/gzip"
14
14
"crypto/sha256"
15
+ "encoding/json"
15
16
"fmt"
16
17
"io"
17
18
"io/ioutil"
@@ -22,7 +23,6 @@ import (
22
23
)
23
24
24
25
const (
25
- currentVersionURL = "https://golang.org/VERSION?m=text"
26
26
downloadURLPrefix = "https://dl.google.com/go"
27
27
)
28
28
@@ -168,18 +168,24 @@ func unpackZip(src, dest string) error {
168
168
}
169
169
170
170
func getLatestGoVersion () (string , error ) {
171
- resp , err := http .Get (currentVersionURL )
171
+ resp , err := http .Get ("https://golang.org/dl/?mode=json" )
172
172
if err != nil {
173
173
return "" , fmt .Errorf ("Getting current Go version failed: %v" , err )
174
174
}
175
175
defer resp .Body .Close ()
176
- if resp .StatusCode > 299 {
176
+ if resp .StatusCode != http . StatusOK {
177
177
b , _ := ioutil .ReadAll (io .LimitReader (resp .Body , 1024 ))
178
- return "" , fmt .Errorf ("Could not get current Go version: HTTP %d: %q" , resp .StatusCode , b )
178
+ return "" , fmt .Errorf ("Could not get current Go release: HTTP %d: %q" , resp .StatusCode , b )
179
+ }
180
+ var releases []struct {
181
+ Version string
179
182
}
180
- version , err := ioutil . ReadAll (resp .Body )
183
+ err = json . NewDecoder (resp .Body ). Decode ( & releases )
181
184
if err != nil {
182
185
return "" , err
183
186
}
184
- return strings .TrimSpace (string (version )), nil
187
+ if len (releases ) < 1 {
188
+ return "" , fmt .Errorf ("Could not get at least one Go release" )
189
+ }
190
+ return releases [0 ].Version , nil
185
191
}
0 commit comments