Skip to content

Commit f22ac5b

Browse files
authored
Merge pull request #19 from carolynvs/download-to-bin
Support downloading to an arbitrary location
2 parents 3031296 + c98b5b2 commit f22ac5b

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed

pkg/downloads/download.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
// PostDownloadHook is the handler called after downloading a file, which returns the absolute path to the binary.
2121
type PostDownloadHook func(archivePath string) (string, error)
2222

23-
// DownloadOptions
23+
// DownloadOptions is the configuration settings used to download a file.
2424
type 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}}
6262
func 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
}

pkg/downloads/download_test.go

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,69 @@
11
package downloads
22

33
import (
4+
"github.com/carolynvs/magex/pkg/gopath"
5+
"github.com/carolynvs/magex/xplat"
46
"github.com/stretchr/testify/assert"
57
"github.com/stretchr/testify/require"
8+
"io/ioutil"
69
"net/http"
710
"net/http/httptest"
11+
"os"
12+
"path/filepath"
813
"testing"
914
)
1015

1116
func TestDownloadToGopathBin(t *testing.T) {
17+
err, cleanup := gopath.UseTempGopath()
18+
require.NoError(t, err, "Failed to set up a temporary GOPATH")
19+
defer cleanup()
20+
21+
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22+
w.Write([]byte("echo ok"))
23+
}))
24+
defer svr.Close()
25+
26+
opts := DownloadOptions{
27+
UrlTemplate: svr.URL,
28+
Name: "mybin",
29+
}
30+
err = DownloadToGopathBin(opts)
31+
require.NoError(t, err)
32+
assert.FileExists(t, filepath.Join(gopath.GetGopathBin(), "mybin"+xplat.FileExt()))
33+
}
34+
35+
func TestDownload(t *testing.T) {
1236
t.Run("not found", func(t *testing.T) {
1337
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1438
w.WriteHeader(404)
1539
}))
1640
defer svr.Close()
1741

18-
t.Run("not found", func(t *testing.T) {
19-
opts := DownloadOptions{
20-
UrlTemplate: svr.URL,
21-
}
22-
err := DownloadToGopathBin(opts)
23-
require.Error(t, err)
24-
assert.Contains(t, err.Error(), "404 Not Found")
25-
})
42+
opts := DownloadOptions{
43+
UrlTemplate: svr.URL,
44+
Name: "mybin",
45+
}
46+
err := Download("bin", opts)
47+
require.Error(t, err)
48+
assert.Contains(t, err.Error(), "404 Not Found")
49+
})
50+
51+
t.Run("found", func(t *testing.T) {
52+
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
53+
w.Write([]byte("echo ok"))
54+
}))
55+
defer svr.Close()
56+
57+
dest, err := ioutil.TempDir("", "magex")
58+
require.NoError(t, err)
59+
defer os.RemoveAll(dest)
60+
61+
opts := DownloadOptions{
62+
UrlTemplate: svr.URL,
63+
Name: "mybin",
64+
}
65+
err = Download(dest, opts)
66+
require.NoError(t, err)
67+
assert.FileExists(t, filepath.Join(dest, "mybin"+xplat.FileExt()))
2668
})
2769
}

0 commit comments

Comments
 (0)