Skip to content

Commit 126df1d

Browse files
fmpwizardtoothrot
authored andcommitted
cmd/getgo: determine current version via /dl/?mode=json API
The getgo tool was using the golang.org version as the latest version to fetch. But this is not always the latest version. We now use https://golang.org/dl/?mode=json which is the official latest version. Fixes golang/go#42676 Change-Id: I1cd90bfba12b19759599e89b7b4a095700999c09 Reviewed-on: https://go-review.googlesource.com/c/tools/+/270878 Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Alexander Rakoczy <alex@golang.org> Trust: Dmitri Shuralyov <dmitshur@golang.org> Trust: Alexander Rakoczy <alex@golang.org> Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org>
1 parent b9b845e commit 126df1d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

cmd/getgo/download.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"archive/zip"
1313
"compress/gzip"
1414
"crypto/sha256"
15+
"encoding/json"
1516
"fmt"
1617
"io"
1718
"io/ioutil"
@@ -22,7 +23,6 @@ import (
2223
)
2324

2425
const (
25-
currentVersionURL = "https://golang.org/VERSION?m=text"
2626
downloadURLPrefix = "https://dl.google.com/go"
2727
)
2828

@@ -168,18 +168,24 @@ func unpackZip(src, dest string) error {
168168
}
169169

170170
func getLatestGoVersion() (string, error) {
171-
resp, err := http.Get(currentVersionURL)
171+
resp, err := http.Get("https://golang.org/dl/?mode=json")
172172
if err != nil {
173173
return "", fmt.Errorf("Getting current Go version failed: %v", err)
174174
}
175175
defer resp.Body.Close()
176-
if resp.StatusCode > 299 {
176+
if resp.StatusCode != http.StatusOK {
177177
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
179182
}
180-
version, err := ioutil.ReadAll(resp.Body)
183+
err = json.NewDecoder(resp.Body).Decode(&releases)
181184
if err != nil {
182185
return "", err
183186
}
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
185191
}

0 commit comments

Comments
 (0)