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>
This commit is contained in:
Diego Medina 2020-11-17 15:34:52 -05:00 коммит произвёл Alexander Rakoczy
Родитель b9b845e62b
Коммит 126df1d64a
1 изменённых файлов: 12 добавлений и 6 удалений

Просмотреть файл

@ -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)
}
version, err := ioutil.ReadAll(resp.Body)
var releases []struct {
Version string
}
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
}