GoPLS Viewer

Home|gopls/cmd/getgo/download.go
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.
4
5//go:build !plan9
6// +build !plan9
7
8package main
9
10import (
11    "archive/tar"
12    "archive/zip"
13    "compress/gzip"
14    "crypto/sha256"
15    "encoding/json"
16    "fmt"
17    "io"
18    "io/ioutil"
19    "net/http"
20    "os"
21    "path/filepath"
22    "strings"
23)
24
25const (
26    downloadURLPrefix = "https://dl.google.com/go"
27)
28
29// downloadGoVersion downloads and upacks the specific go version to dest/go.
30func downloadGoVersion(versionopsarchdest stringerror {
31    suffix := "tar.gz"
32    if ops == "windows" {
33        suffix = "zip"
34    }
35    uri := fmt.Sprintf("%s/%s.%s-%s.%s"downloadURLPrefixversionopsarchsuffix)
36
37    verbosef("Downloading %s"uri)
38
39    reqerr := http.NewRequest("GET"urinil)
40    if err != nil {
41        return err
42    }
43    req.Header.Add("User-Agent"fmt.Sprintf("golang.org-getgo/%s"version))
44
45    resperr := http.DefaultClient.Do(req)
46    if err != nil {
47        return fmt.Errorf("Downloading Go from %s failed: %v"urierr)
48    }
49    if resp.StatusCode > 299 {
50        return fmt.Errorf("Downloading Go from %s failed with HTTP status %s"uriresp.Status)
51    }
52    defer resp.Body.Close()
53
54    tmpferr := ioutil.TempFile("""go")
55    if err != nil {
56        return err
57    }
58    defer os.Remove(tmpf.Name())
59
60    h := sha256.New()
61
62    w := io.MultiWriter(tmpfh)
63    if _err := io.Copy(wresp.Body); err != nil {
64        return err
65    }
66
67    verbosef("Downloading SHA %s.sha256"uri)
68
69    sresperr := http.Get(uri + ".sha256")
70    if err != nil {
71        return fmt.Errorf("Downloading Go sha256 from %s.sha256 failed: %v"urierr)
72    }
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"urisresp.Status)
76    }
77
78    shasumerr := ioutil.ReadAll(sresp.Body)
79    if err != nil {
80        return err
81    }
82
83    // Check the shasum.
84    sum := fmt.Sprintf("%x"h.Sum(nil))
85    if sum != string(shasum) {
86        return fmt.Errorf("Shasum mismatch %s vs. %s"sumstring(shasum))
87    }
88
89    unpackFunc := unpackTar
90    if ops == "windows" {
91        unpackFunc = unpackZip
92    }
93    if err := unpackFunc(tmpf.Name(), dest); err != nil {
94        return fmt.Errorf("Unpacking Go to %s failed: %v"desterr)
95    }
96    return nil
97}
98
99func unpack(destname stringfi os.FileInfor io.Readererror {
100    if strings.HasPrefix(name"go/") {
101        name = name[len("go/"):]
102    }
103
104    path := filepath.Join(destname)
105    if fi.IsDir() {
106        return os.MkdirAll(pathfi.Mode())
107    }
108
109    ferr := os.OpenFile(pathos.O_CREATE|os.O_TRUNC|os.O_WRONLYfi.Mode())
110    if err != nil {
111        return err
112    }
113    defer f.Close()
114
115    _err = io.Copy(fr)
116    return err
117}
118
119func unpackTar(srcdest stringerror {
120    rerr := os.Open(src)
121    if err != nil {
122        return err
123    }
124    defer r.Close()
125
126    archiveerr := gzip.NewReader(r)
127    if err != nil {
128        return err
129    }
130    defer archive.Close()
131
132    tarReader := tar.NewReader(archive)
133
134    for {
135        headererr := tarReader.Next()
136        if err == io.EOF {
137            break
138        } else if err != nil {
139            return err
140        }
141
142        if err := unpack(destheader.Nameheader.FileInfo(), tarReader); err != nil {
143            return err
144        }
145    }
146
147    return nil
148}
149
150func unpackZip(srcdest stringerror {
151    zrerr := zip.OpenReader(src)
152    if err != nil {
153        return err
154    }
155
156    for _f := range zr.File {
157        frerr := f.Open()
158        if err != nil {
159            return err
160        }
161        if err := unpack(destf.Namef.FileInfo(), fr); err != nil {
162            return err
163        }
164        fr.Close()
165    }
166
167    return nil
168}
169
170func getLatestGoVersion() (stringerror) {
171    resperr := http.Get("https://golang.org/dl/?mode=json")
172    if err != nil {
173        return ""fmt.Errorf("Getting current Go version failed: %v"err)
174    }
175    defer resp.Body.Close()
176    if resp.StatusCode != http.StatusOK {
177        b_ := ioutil.ReadAll(io.LimitReader(resp.Body1024))
178        return ""fmt.Errorf("Could not get current Go release: HTTP %d: %q"resp.StatusCodeb)
179    }
180    var releases []struct {
181        Version string
182    }
183    err = json.NewDecoder(resp.Body).Decode(&releases)
184    if err != nil {
185        return ""err
186    }
187    if len(releases) < 1 {
188        return ""fmt.Errorf("Could not get at least one Go release")
189    }
190    return releases[0].Versionnil
191}
192
MembersX
filepath
downloadURLPrefix
downloadGoVersion.h
unpack.name
unpack.f
unpackTar.dest
unpackTar.BlockStmt.err
json
getLatestGoVersion.BlockStmt.b
downloadGoVersion._
unpack.r
http
downloadGoVersion.tmpf
downloadGoVersion.w
zip
downloadGoVersion.shasum
getLatestGoVersion.resp
ioutil
downloadGoVersion.ops
unpack.path
unpack.err
getLatestGoVersion.BlockStmt._
downloadGoVersion
downloadGoVersion.arch
downloadGoVersion.sresp
unpackZip.src
getLatestGoVersion
tar
downloadGoVersion.dest
downloadGoVersion.err
unpackTar.err
unpackZip.err
os
unpackZip.dest
unpack
downloadGoVersion.req
unpackTar
unpackZip
unpackZip.RangeStmt_3200.BlockStmt.err
gzip
downloadGoVersion.sum
unpackTar.archive
downloadGoVersion.suffix
unpack.dest
unpackTar.BlockStmt.header
downloadGoVersion.unpackFunc
downloadGoVersion.uri
unpackTar.src
strings
unpack.fi
unpackZip.RangeStmt_3200.BlockStmt.fr
fmt
downloadGoVersion.version
sha256
downloadGoVersion.resp
unpackTar.r
unpackTar.tarReader
unpackZip.zr
unpackZip.RangeStmt_3200.f
getLatestGoVersion.err
io
Members
X