Skip to content

Commit

Permalink
Merge pull request #19 from carolynvs/download-to-bin
Browse files Browse the repository at this point in the history
Support downloading to an arbitrary location
  • Loading branch information
carolynvs authored Apr 8, 2022
2 parents 3031296 + c98b5b2 commit f22ac5b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
22 changes: 13 additions & 9 deletions pkg/downloads/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// PostDownloadHook is the handler called after downloading a file, which returns the absolute path to the binary.
type PostDownloadHook func(archivePath string) (string, error)

// DownloadOptions
// DownloadOptions is the configuration settings used to download a file.
type DownloadOptions struct {
// UrlTemplate is the Go template for the URL to download. Required.
// Available Template Variables:
Expand Down Expand Up @@ -60,16 +60,20 @@ type DownloadOptions struct {
// - {{.EXT}}
// - {{.VERSION}}
func DownloadToGopathBin(opts DownloadOptions) error {
src, err := RenderTemplate(opts.UrlTemplate, opts)
if err != nil {

if err := gopath.EnsureGopathBin(); err != nil {
return err
}
log.Printf("Downloading %s...", src)
bin := gopath.GetGopathBin()
return Download(bin, opts)
}

err = gopath.EnsureGopathBin()
func Download(destDir string, opts DownloadOptions) error {
src, err := RenderTemplate(opts.UrlTemplate, opts)
if err != nil {
return err
}
log.Printf("Downloading %s...", src)

// Download to a temp file
tmpDir, err := ioutil.TempDir("", "magex")
Expand Down Expand Up @@ -116,10 +120,10 @@ func DownloadToGopathBin(opts DownloadOptions) error {
return fmt.Errorf("could not make %s executable: %w", tmpBin, err)
}

// Move it to GOPATH/bin
dest := filepath.Join(gopath.GetGopathBin(), opts.Name+xplat.FileExt())
if err := shx.Copy(tmpBin, dest); err != nil {
return fmt.Errorf("error copying %s to %s: %w", tmpBin, dest, err)
// Move it to the destination
destPath := filepath.Join(destDir, opts.Name+xplat.FileExt())
if err := shx.Copy(tmpBin, destPath); err != nil {
return fmt.Errorf("error copying %s to %s: %w", tmpBin, destPath, err)
}
return nil
}
Expand Down
58 changes: 50 additions & 8 deletions pkg/downloads/download_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,69 @@
package downloads

import (
"github.com/carolynvs/magex/pkg/gopath"
"github.com/carolynvs/magex/xplat"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)

func TestDownloadToGopathBin(t *testing.T) {
err, cleanup := gopath.UseTempGopath()
require.NoError(t, err, "Failed to set up a temporary GOPATH")
defer cleanup()

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("echo ok"))
}))
defer svr.Close()

opts := DownloadOptions{
UrlTemplate: svr.URL,
Name: "mybin",
}
err = DownloadToGopathBin(opts)
require.NoError(t, err)
assert.FileExists(t, filepath.Join(gopath.GetGopathBin(), "mybin"+xplat.FileExt()))
}

func TestDownload(t *testing.T) {
t.Run("not found", func(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
}))
defer svr.Close()

t.Run("not found", func(t *testing.T) {
opts := DownloadOptions{
UrlTemplate: svr.URL,
}
err := DownloadToGopathBin(opts)
require.Error(t, err)
assert.Contains(t, err.Error(), "404 Not Found")
})
opts := DownloadOptions{
UrlTemplate: svr.URL,
Name: "mybin",
}
err := Download("bin", opts)
require.Error(t, err)
assert.Contains(t, err.Error(), "404 Not Found")
})

t.Run("found", func(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("echo ok"))
}))
defer svr.Close()

dest, err := ioutil.TempDir("", "magex")
require.NoError(t, err)
defer os.RemoveAll(dest)

opts := DownloadOptions{
UrlTemplate: svr.URL,
Name: "mybin",
}
err = Download(dest, opts)
require.NoError(t, err)
assert.FileExists(t, filepath.Join(dest, "mybin"+xplat.FileExt()))
})
}

0 comments on commit f22ac5b

Please sign in to comment.