|
| 1 | +// Copyright 2018 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 | +package modcmd |
| 6 | + |
| 7 | +import ( |
| 8 | + "cmd/go/internal/base" |
| 9 | + "cmd/go/internal/modfetch" |
| 10 | + "cmd/go/internal/modload" |
| 11 | + "cmd/go/internal/module" |
| 12 | + "cmd/go/internal/par" |
| 13 | + "encoding/json" |
| 14 | + "os" |
| 15 | +) |
| 16 | + |
| 17 | +var cmdDownload = &base.Command{ |
| 18 | + UsageLine: "go mod download [-dir] [-json] [modules]", |
| 19 | + Short: "download modules to local cache", |
| 20 | + Long: ` |
| 21 | +Download downloads the named modules, which can be module patterns selecting |
| 22 | +dependencies of the main module or module queries of the form path@version. |
| 23 | +With no arguments, download applies to all dependencies of the main module. |
| 24 | +
|
| 25 | +The go command will automatically download modules as needed during ordinary |
| 26 | +execution. The "go mod download" command is useful mainly for pre-filling |
| 27 | +the local cache or to compute the answers for a Go module proxy. |
| 28 | +
|
| 29 | +By default, download reports errors to standard error but is otherwise silent. |
| 30 | +The -json flag causes download to print a sequence of JSON objects |
| 31 | +to standard output, describing each downloaded module (or failure), |
| 32 | +corresponding to this Go struct: |
| 33 | +
|
| 34 | + type Module struct { |
| 35 | + Path string // module path |
| 36 | + Version string // module version |
| 37 | + Error string // error loading module |
| 38 | + Info string // absolute path to cached .info file |
| 39 | + GoMod string // absolute path to cached .mod file |
| 40 | + Zip string // absolute path to cached .zip file |
| 41 | + Dir string // absolute path to cached source root directory |
| 42 | + } |
| 43 | +
|
| 44 | +See 'go help module' for more about module queries. |
| 45 | + `, |
| 46 | +} |
| 47 | + |
| 48 | +var downloadJSON = cmdDownload.Flag.Bool("json", false, "") |
| 49 | + |
| 50 | +func init() { |
| 51 | + cmdDownload.Run = runDownload // break init cycle |
| 52 | +} |
| 53 | + |
| 54 | +type moduleJSON struct { |
| 55 | + Path string `json:",omitempty"` |
| 56 | + Version string `json:",omitempty"` |
| 57 | + Error string `json:",omitempty"` |
| 58 | + Info string `json:",omitempty"` |
| 59 | + GoMod string `json:",omitempty"` |
| 60 | + Zip string `json:",omitempty"` |
| 61 | + Dir string `json:",omitempty"` |
| 62 | +} |
| 63 | + |
| 64 | +func runDownload(cmd *base.Command, args []string) { |
| 65 | + if len(args) == 0 { |
| 66 | + args = []string{"all"} |
| 67 | + } |
| 68 | + |
| 69 | + var mods []*moduleJSON |
| 70 | + var work par.Work |
| 71 | + listU := false |
| 72 | + listVersions := false |
| 73 | + for _, info := range modload.ListModules(args, listU, listVersions) { |
| 74 | + if info.Replace != nil { |
| 75 | + info = info.Replace |
| 76 | + } |
| 77 | + if info.Version == "" { |
| 78 | + continue |
| 79 | + } |
| 80 | + m := &moduleJSON{ |
| 81 | + Path: info.Path, |
| 82 | + Version: info.Version, |
| 83 | + } |
| 84 | + mods = append(mods, m) |
| 85 | + work.Add(m) |
| 86 | + } |
| 87 | + |
| 88 | + work.Do(10, func(item interface{}) { |
| 89 | + m := item.(*moduleJSON) |
| 90 | + var err error |
| 91 | + m.Info, err = modfetch.InfoFile(m.Path, m.Version) |
| 92 | + if err != nil { |
| 93 | + m.Error = err.Error() |
| 94 | + return |
| 95 | + } |
| 96 | + m.GoMod, err = modfetch.GoModFile(m.Path, m.Version) |
| 97 | + if err != nil { |
| 98 | + m.Error = err.Error() |
| 99 | + return |
| 100 | + } |
| 101 | + mod := module.Version{Path: m.Path, Version: m.Version} |
| 102 | + m.Zip, err = modfetch.DownloadZip(mod) |
| 103 | + if err != nil { |
| 104 | + m.Error = err.Error() |
| 105 | + return |
| 106 | + } |
| 107 | + m.Dir, err = modfetch.Download(mod) |
| 108 | + if err != nil { |
| 109 | + m.Error = err.Error() |
| 110 | + return |
| 111 | + } |
| 112 | + }) |
| 113 | + |
| 114 | + if *downloadJSON { |
| 115 | + for _, m := range mods { |
| 116 | + b, err := json.MarshalIndent(m, "", "\t") |
| 117 | + if err != nil { |
| 118 | + base.Fatalf("%v", err) |
| 119 | + } |
| 120 | + os.Stdout.Write(append(b, '\n')) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments