Source file src/pkg/cmd/go/internal/modcmd/download.go
1
2
3
4
5 package modcmd
6
7 import (
8 "cmd/go/internal/cfg"
9 "encoding/json"
10 "os"
11
12 "cmd/go/internal/base"
13 "cmd/go/internal/modfetch"
14 "cmd/go/internal/modload"
15 "cmd/go/internal/module"
16 "cmd/go/internal/par"
17 )
18
19 var cmdDownload = &base.Command{
20 UsageLine: "go mod download [-json] [modules]",
21 Short: "download modules to local cache",
22 Long: `
23 Download downloads the named modules, which can be module patterns selecting
24 dependencies of the main module or module queries of the form path@version.
25 With no arguments, download applies to all dependencies of the main module.
26
27 The go command will automatically download modules as needed during ordinary
28 execution. The "go mod download" command is useful mainly for pre-filling
29 the local cache or to compute the answers for a Go module proxy.
30
31 By default, download reports errors to standard error but is otherwise silent.
32 The -json flag causes download to print a sequence of JSON objects
33 to standard output, describing each downloaded module (or failure),
34 corresponding to this Go struct:
35
36 type Module struct {
37 Path string // module path
38 Version string // module version
39 Error string // error loading module
40 Info string // absolute path to cached .info file
41 GoMod string // absolute path to cached .mod file
42 Zip string // absolute path to cached .zip file
43 Dir string // absolute path to cached source root directory
44 Sum string // checksum for path, version (as in go.sum)
45 GoModSum string // checksum for go.mod (as in go.sum)
46 }
47
48 See 'go help modules' for more about module queries.
49 `,
50 }
51
52 var downloadJSON = cmdDownload.Flag.Bool("json", false, "")
53
54 func init() {
55 cmdDownload.Run = runDownload
56 }
57
58 type moduleJSON struct {
59 Path string `json:",omitempty"`
60 Version string `json:",omitempty"`
61 Error string `json:",omitempty"`
62 Info string `json:",omitempty"`
63 GoMod string `json:",omitempty"`
64 Zip string `json:",omitempty"`
65 Dir string `json:",omitempty"`
66 Sum string `json:",omitempty"`
67 GoModSum string `json:",omitempty"`
68 }
69
70 func runDownload(cmd *base.Command, args []string) {
71
72 if cfg.Getenv("GO111MODULE") == "off" {
73 base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
74 }
75 if !modload.HasModRoot() && len(args) == 0 {
76 base.Fatalf("go mod download: no modules specified (see 'go help mod download')")
77 }
78 if len(args) == 0 {
79 args = []string{"all"}
80 }
81
82 var mods []*moduleJSON
83 var work par.Work
84 listU := false
85 listVersions := false
86 for _, info := range modload.ListModules(args, listU, listVersions) {
87 if info.Replace != nil {
88 info = info.Replace
89 }
90 if info.Version == "" && info.Error == nil {
91
92 continue
93 }
94 m := &moduleJSON{
95 Path: info.Path,
96 Version: info.Version,
97 }
98 mods = append(mods, m)
99 if info.Error != nil {
100 m.Error = info.Error.Err
101 continue
102 }
103 work.Add(m)
104 }
105
106 work.Do(10, func(item interface{}) {
107 m := item.(*moduleJSON)
108 var err error
109 m.Info, err = modfetch.InfoFile(m.Path, m.Version)
110 if err != nil {
111 m.Error = err.Error()
112 return
113 }
114 m.GoMod, err = modfetch.GoModFile(m.Path, m.Version)
115 if err != nil {
116 m.Error = err.Error()
117 return
118 }
119 m.GoModSum, err = modfetch.GoModSum(m.Path, m.Version)
120 if err != nil {
121 m.Error = err.Error()
122 return
123 }
124 mod := module.Version{Path: m.Path, Version: m.Version}
125 m.Zip, err = modfetch.DownloadZip(mod)
126 if err != nil {
127 m.Error = err.Error()
128 return
129 }
130 m.Sum = modfetch.Sum(mod)
131 m.Dir, err = modfetch.Download(mod)
132 if err != nil {
133 m.Error = err.Error()
134 return
135 }
136 })
137
138 if *downloadJSON {
139 for _, m := range mods {
140 b, err := json.MarshalIndent(m, "", "\t")
141 if err != nil {
142 base.Fatalf("%v", err)
143 }
144 os.Stdout.Write(append(b, '\n'))
145 if m.Error != "" {
146 base.SetExitStatus(1)
147 }
148 }
149 } else {
150 for _, m := range mods {
151 if m.Error != "" {
152 base.Errorf("%s", m.Error)
153 }
154 }
155 base.ExitIfErrors()
156 }
157 }
158
View as plain text