@@ -20,7 +20,7 @@ import (
2020// PostDownloadHook is the handler called after downloading a file, which returns the absolute path to the binary.
2121type PostDownloadHook func (archivePath string ) (string , error )
2222
23- // DownloadOptions
23+ // DownloadOptions is the configuration settings used to download a file.
2424type DownloadOptions struct {
2525 // UrlTemplate is the Go template for the URL to download. Required.
2626 // Available Template Variables:
@@ -60,16 +60,20 @@ type DownloadOptions struct {
6060// - {{.EXT}}
6161// - {{.VERSION}}
6262func DownloadToGopathBin (opts DownloadOptions ) error {
63- src , err := RenderTemplate ( opts . UrlTemplate , opts )
64- if err != nil {
63+
64+ if err := gopath . EnsureGopathBin (); err != nil {
6565 return err
6666 }
67- log .Printf ("Downloading %s..." , src )
67+ bin := gopath .GetGopathBin ()
68+ return Download (bin , opts )
69+ }
6870
69- err = gopath .EnsureGopathBin ()
71+ func Download (destDir string , opts DownloadOptions ) error {
72+ src , err := RenderTemplate (opts .UrlTemplate , opts )
7073 if err != nil {
7174 return err
7275 }
76+ log .Printf ("Downloading %s..." , src )
7377
7478 // Download to a temp file
7579 tmpDir , err := ioutil .TempDir ("" , "magex" )
@@ -116,10 +120,10 @@ func DownloadToGopathBin(opts DownloadOptions) error {
116120 return fmt .Errorf ("could not make %s executable: %w" , tmpBin , err )
117121 }
118122
119- // Move it to GOPATH/bin
120- dest := filepath .Join (gopath . GetGopathBin () , opts .Name + xplat .FileExt ())
121- if err := shx .Copy (tmpBin , dest ); err != nil {
122- return fmt .Errorf ("error copying %s to %s: %w" , tmpBin , dest , err )
123+ // Move it to the destination
124+ destPath := filepath .Join (destDir , opts .Name + xplat .FileExt ())
125+ if err := shx .Copy (tmpBin , destPath ); err != nil {
126+ return fmt .Errorf ("error copying %s to %s: %w" , tmpBin , destPath , err )
123127 }
124128 return nil
125129}
0 commit comments