github.com/sirkon/goproxy@v1.4.8/internal/modcmd/download.go (about)

     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  	"github.com/sirkon/goproxy/internal/base"
     9  	"github.com/sirkon/goproxy/internal/modfetch"
    10  	"github.com/sirkon/goproxy/internal/modload"
    11  	"github.com/sirkon/goproxy/internal/module"
    12  	"github.com/sirkon/goproxy/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          Sum      string // checksum for path, version (as in go.sum)
    43          GoModSum string // checksum for go.mod (as in go.sum)
    44      }
    45  
    46  See 'go help modules' for more about module queries.
    47  	`,
    48  }
    49  
    50  var downloadJSON = cmdDownload.Flag.Bool("json", false, "")
    51  
    52  func init() {
    53  	cmdDownload.Run = runDownload // break init cycle
    54  }
    55  
    56  type moduleJSON struct {
    57  	Path     string `json:",omitempty"`
    58  	Version  string `json:",omitempty"`
    59  	Error    string `json:",omitempty"`
    60  	Info     string `json:",omitempty"`
    61  	GoMod    string `json:",omitempty"`
    62  	Zip      string `json:",omitempty"`
    63  	Dir      string `json:",omitempty"`
    64  	Sum      string `json:",omitempty"`
    65  	GoModSum string `json:",omitempty"`
    66  }
    67  
    68  func runDownload(cmd *base.Command, args []string) {
    69  	if len(args) == 0 {
    70  		args = []string{"all"}
    71  	}
    72  
    73  	var mods []*moduleJSON
    74  	var work par.Work
    75  	listU := false
    76  	listVersions := false
    77  	for _, info := range modload.ListModules(args, listU, listVersions) {
    78  		if info.Replace != nil {
    79  			info = info.Replace
    80  		}
    81  		if info.Version == "" {
    82  			continue
    83  		}
    84  		m := &moduleJSON{
    85  			Path:    info.Path,
    86  			Version: info.Version,
    87  		}
    88  		mods = append(mods, m)
    89  		work.Add(m)
    90  	}
    91  
    92  	work.Do(10, func(item interface{}) {
    93  		m := item.(*moduleJSON)
    94  		var err error
    95  		m.Info, err = modfetch.InfoFile(m.Path, m.Version)
    96  		if err != nil {
    97  			m.Error = err.Error()
    98  			return
    99  		}
   100  		m.GoMod, err = modfetch.GoModFile(m.Path, m.Version)
   101  		if err != nil {
   102  			m.Error = err.Error()
   103  			return
   104  		}
   105  		m.GoModSum, err = modfetch.GoModSum(m.Path, m.Version)
   106  		if err != nil {
   107  			m.Error = err.Error()
   108  			return
   109  		}
   110  		mod := module.Version{Path: m.Path, Version: m.Version}
   111  		m.Zip, err = modfetch.DownloadZip(mod)
   112  		if err != nil {
   113  			m.Error = err.Error()
   114  			return
   115  		}
   116  		m.Sum = modfetch.Sum(mod)
   117  		m.Dir, err = modfetch.Download(mod)
   118  		if err != nil {
   119  			m.Error = err.Error()
   120  			return
   121  		}
   122  	})
   123  
   124  	if *downloadJSON {
   125  		for _, m := range mods {
   126  			b, err := json.MarshalIndent(m, "", "\t")
   127  			if err != nil {
   128  				base.Fatalf("%v", err)
   129  			}
   130  			os.Stdout.Write(append(b, '\n'))
   131  		}
   132  	}
   133  }