Skip to content

Commit

Permalink
cmd/getgo: determine current version via /dl/?mode=json API
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Alexander Rakoczy <[email protected]>
Trust: Dmitri Shuralyov <[email protected]>
Trust: Alexander Rakoczy <[email protected]>
Run-TryBot: Dmitri Shuralyov <[email protected]>
gopls-CI: kokoro <[email protected]>
TryBot-Result: Go Bot <[email protected]>
  • Loading branch information
fmpwizard authored and toothrot committed Jun 7, 2021
1 parent b9b845e commit 126df1d
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions cmd/getgo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"archive/zip"
"compress/gzip"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand All @@ -22,7 +23,6 @@ import (
)

const (
currentVersionURL = "https://golang.org/VERSION?m=text"
downloadURLPrefix = "https://dl.google.com/go"
)

Expand Down Expand Up @@ -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
}

0 comments on commit 126df1d

Please sign in to comment.