1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
25 currentVersionURL = "https://golang.org/VERSION?m=text"
26 downloadURLPrefix = "https://dl.google.com/go"
29 // downloadGoVersion downloads and upacks the specific go version to dest/go.
30 func downloadGoVersion(version, ops, arch, dest string) error {
35 uri := fmt.Sprintf("%s/%s.%s-%s.%s", downloadURLPrefix, version, ops, arch, suffix)
37 verbosef("Downloading %s", uri)
39 req, err := http.NewRequest("GET", uri, nil)
43 req.Header.Add("User-Agent", fmt.Sprintf("golang.org-getgo/%s", version))
45 resp, err := http.DefaultClient.Do(req)
47 return fmt.Errorf("Downloading Go from %s failed: %v", uri, err)
49 if resp.StatusCode > 299 {
50 return fmt.Errorf("Downloading Go from %s failed with HTTP status %s", uri, resp.Status)
52 defer resp.Body.Close()
54 tmpf, err := ioutil.TempFile("", "go")
58 defer os.Remove(tmpf.Name())
62 w := io.MultiWriter(tmpf, h)
63 if _, err := io.Copy(w, resp.Body); err != nil {
67 verbosef("Downloading SHA %s.sha256", uri)
69 sresp, err := http.Get(uri + ".sha256")
71 return fmt.Errorf("Downloading Go sha256 from %s.sha256 failed: %v", uri, err)
73 defer sresp.Body.Close()
74 if sresp.StatusCode > 299 {
75 return fmt.Errorf("Downloading Go sha256 from %s.sha256 failed with HTTP status %s", uri, sresp.Status)
78 shasum, err := ioutil.ReadAll(sresp.Body)
84 sum := fmt.Sprintf("%x", h.Sum(nil))
85 if sum != string(shasum) {
86 return fmt.Errorf("Shasum mismatch %s vs. %s", sum, string(shasum))
89 unpackFunc := unpackTar
91 unpackFunc = unpackZip
93 if err := unpackFunc(tmpf.Name(), dest); err != nil {
94 return fmt.Errorf("Unpacking Go to %s failed: %v", dest, err)
99 func unpack(dest, name string, fi os.FileInfo, r io.Reader) error {
100 if strings.HasPrefix(name, "go/") {
101 name = name[len("go/"):]
104 path := filepath.Join(dest, name)
106 return os.MkdirAll(path, fi.Mode())
109 f, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, fi.Mode())
115 _, err = io.Copy(f, r)
119 func unpackTar(src, dest string) error {
120 r, err := os.Open(src)
126 archive, err := gzip.NewReader(r)
130 defer archive.Close()
132 tarReader := tar.NewReader(archive)
135 header, err := tarReader.Next()
138 } else if err != nil {
142 if err := unpack(dest, header.Name, header.FileInfo(), tarReader); err != nil {
150 func unpackZip(src, dest string) error {
151 zr, err := zip.OpenReader(src)
156 for _, f := range zr.File {
161 if err := unpack(dest, f.Name, f.FileInfo(), fr); err != nil {
170 func getLatestGoVersion() (string, error) {
171 resp, err := http.Get(currentVersionURL)
173 return "", fmt.Errorf("Getting current Go version failed: %v", err)
175 defer resp.Body.Close()
176 if resp.StatusCode > 299 {
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)
180 version, err := ioutil.ReadAll(resp.Body)
184 return strings.TrimSpace(string(version)), nil