From 126df1d64aadd0f03b393316837fd045b75e862a Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Tue, 17 Nov 2020 15:34:52 -0500 Subject: [PATCH] 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 Reviewed-by: Alexander Rakoczy Trust: Dmitri Shuralyov Trust: Alexander Rakoczy Run-TryBot: Dmitri Shuralyov gopls-CI: kokoro TryBot-Result: Go Bot --- cmd/getgo/download.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/cmd/getgo/download.go b/cmd/getgo/download.go index 1731131d857..86f0a2fed80 100644 --- a/cmd/getgo/download.go +++ b/cmd/getgo/download.go @@ -12,6 +12,7 @@ import ( "archive/zip" "compress/gzip" "crypto/sha256" + "encoding/json" "fmt" "io" "io/ioutil" @@ -22,7 +23,6 @@ import ( ) const ( - currentVersionURL = "https://golang.org/VERSION?m=text" downloadURLPrefix = "https://dl.google.com/go" ) @@ -168,18 +168,24 @@ func unpackZip(src, dest string) error { } func getLatestGoVersion() (string, error) { - resp, err := http.Get(currentVersionURL) + resp, err := http.Get("https://golang.org/dl/?mode=json") if err != nil { return "", fmt.Errorf("Getting current Go version failed: %v", err) } defer resp.Body.Close() - if resp.StatusCode > 299 { + if resp.StatusCode != http.StatusOK { b, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 1024)) - return "", fmt.Errorf("Could not get current Go version: HTTP %d: %q", resp.StatusCode, b) + return "", fmt.Errorf("Could not get current Go release: HTTP %d: %q", resp.StatusCode, b) + } + var releases []struct { + Version string } - version, err := ioutil.ReadAll(resp.Body) + err = json.NewDecoder(resp.Body).Decode(&releases) if err != nil { return "", err } - return strings.TrimSpace(string(version)), nil + if len(releases) < 1 { + return "", fmt.Errorf("Could not get at least one Go release") + } + return releases[0].Version, nil }