-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DownloadToGopathBin for archived files (#5)
Add DownloadToGopathBin for archived files 🚨 THIS HAS BREAKING CHANGES! * Move GOPATH related functions to github.com/carolynvs/magex/gopath * Add package github.com/carolynvs/magex/archive to handle downloading an archive to your GOPATH/bin directory. It is in it's own package so that you don't have to add the dependencies to support archives unless you are using it.
- Loading branch information
Showing
12 changed files
with
440 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Helper methods for working with archived/compressed files. | ||
// | ||
// These functions are separated into their own package to | ||
// limit the dependencies pulled in when using magex if you | ||
// are not using archived files. | ||
package archive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package archive | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/carolynvs/magex/pkg/downloads" | ||
"github.com/carolynvs/magex/xplat" | ||
"github.com/mholt/archiver/v3" | ||
_ "github.com/mholt/archiver/v3" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// DownloadArchiveOptions are the set of options available for DownloadToGopathBin. | ||
type DownloadArchiveOptions struct { | ||
downloads.DownloadOptions | ||
|
||
// ArchiveExtensions maps from the GOOS to the expected extension. Required. | ||
// For example, windows may use .zip while darwin/linux uses .tgz. | ||
ArchiveExtensions map[string]string | ||
|
||
// TargetFileTemplate specifies the path to the target binary in the archive. Required. | ||
// Supports the same templating as downloads.DownloadOptions.UrlTemplate. | ||
TargetFileTemplate string | ||
} | ||
|
||
// DownloadToGopathBin downloads an archived file to GOPATH/bin. | ||
func DownloadToGopathBin(opts DownloadArchiveOptions) error { | ||
// determine the appropriate file extension based on the OS, e.g. windows gets .zip, otherwise .tgz | ||
opts.Ext = opts.ArchiveExtensions[runtime.GOOS] | ||
if opts.Ext == "" { | ||
return errors.Errorf("no archive file extension was specified for the current GOOS (%s)", runtime.GOOS) | ||
} | ||
|
||
if opts.Hook == nil { | ||
opts.Hook = ExtractBinaryFromArchiveHook(opts) | ||
} | ||
|
||
return downloads.DownloadToGopathBin(opts.DownloadOptions) | ||
} | ||
|
||
// ExtractBinaryFromArchiveHook is the default hook for DownloadToGopathBin. | ||
func ExtractBinaryFromArchiveHook(opts DownloadArchiveOptions) downloads.PostDownloadHook { | ||
return func(archiveFile string) (binPath string, err error) { | ||
// Save the binary next to the archive file in the temp directory | ||
outDir := filepath.Dir(archiveFile) | ||
|
||
// Render the name of the file in the archive | ||
opts.Ext = xplat.FileExt() | ||
targetFile, err := downloads.RenderTemplate(opts.TargetFileTemplate, opts.DownloadOptions) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "error rendering TargetFileTemplate") | ||
} | ||
|
||
log.Printf("extracting %s from %s...\n", targetFile, archiveFile) | ||
|
||
// Extract the binary | ||
err = archiver.Extract(archiveFile, targetFile, outDir) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "unable to unpack %s", archiveFile) | ||
} | ||
|
||
// The extracted file may be nested depending on its position in the archive | ||
binFile := filepath.Join(outDir, targetFile) | ||
|
||
// Check that file was extracted, Extract doesn't error out if you give it a missing targetFile | ||
if _, err := os.Stat(binFile); os.IsNotExist(err) { | ||
return "", errors.Errorf("could not find %s in the archive", targetFile) | ||
} | ||
|
||
return binFile, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package archive_test | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/carolynvs/magex/pkg/archive" | ||
"github.com/carolynvs/magex/pkg/downloads" | ||
"github.com/carolynvs/magex/pkg/gopath" | ||
) | ||
|
||
func ExampleDownloadToGopathBin() { | ||
opts := archive.DownloadArchiveOptions{ | ||
DownloadOptions: downloads.DownloadOptions{ | ||
UrlTemplate: "https://get.helm.sh/helm-{{.VERSION}}-{{.GOOS}}-{{.GOARCH}}{{.EXT}}", | ||
Name: "helm", | ||
Version: "v3.5.3", | ||
}, | ||
ArchiveExtensions: map[string]string{ | ||
"darwin": ".tar.gz", | ||
"linux": ".tar.gz", | ||
"windows": ".zip", | ||
}, | ||
TargetFileTemplate: "{{.GOOS}}-{{.GOARCH}}/helm{{.EXT}}", | ||
} | ||
err := archive.DownloadToGopathBin(opts) | ||
if err != nil { | ||
log.Fatal("could not download helm") | ||
} | ||
|
||
// Add GOPATH/bin to PATH if necessary so that we can immediately | ||
// use the installed tool | ||
gopath.EnsureGopathBin() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package archive | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/carolynvs/magex/pkg/downloads" | ||
"github.com/carolynvs/magex/pkg/gopath" | ||
"github.com/carolynvs/magex/xplat" | ||
"github.com/magefile/mage/mg" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDownloadArchiveToGopathBin(t *testing.T) { | ||
os.Setenv(mg.VerboseEnv, "true") | ||
err, cleanup := gopath.UseTempGopath() | ||
require.NoError(t, err, "Failed to set up a temporary GOPATH") | ||
defer cleanup() | ||
|
||
// gh cli unfortunately uses a different archive schema depending on the OS | ||
tmpl := "gh_{{.VERSION}}_{{.GOOS}}_{{.GOARCH}}/bin/gh{{.EXT}}" | ||
if runtime.GOOS == "windows" { | ||
tmpl = "bin/gh.exe" | ||
} | ||
|
||
opts := DownloadArchiveOptions{ | ||
DownloadOptions: downloads.DownloadOptions{ | ||
UrlTemplate: "https://github.com/cli/cli/releases/download/v{{.VERSION}}/gh_{{.VERSION}}_{{.GOOS}}_{{.GOARCH}}{{.EXT}}", | ||
Name: "gh", | ||
Version: "1.8.1", | ||
OsReplacement: map[string]string{ | ||
"darwin": "macOS", | ||
}, | ||
}, | ||
ArchiveExtensions: map[string]string{ | ||
"linux": ".tar.gz", | ||
"darwin": ".tar.gz", | ||
"windows": ".zip", | ||
}, | ||
TargetFileTemplate: tmpl, | ||
} | ||
|
||
err = DownloadToGopathBin(opts) | ||
require.NoError(t, err) | ||
|
||
_, err = exec.LookPath("gh" + xplat.FileExt()) | ||
require.NoError(t, err) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package downloads | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"text/template" | ||
|
||
"github.com/carolynvs/magex/pkg/gopath" | ||
"github.com/carolynvs/magex/shx" | ||
"github.com/carolynvs/magex/xplat" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// 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 | ||
type DownloadOptions struct { | ||
// UrlTemplate is the Go template for the URL to download. Required. | ||
// Available Template Variables: | ||
// - {{.GOOS}} | ||
// - {{.GOARCH}} | ||
// - {{.EXT}} | ||
// - {{.VERSION}} | ||
UrlTemplate string | ||
|
||
// Name of the binary, excluding OS specific file extension. Required. | ||
Name string | ||
|
||
// Version to replace {{.VERSION}} in the URL template. Optional depending on whether or not the version is in the UrlTemplate. | ||
Version string | ||
|
||
// Ext to replace {{.EXT}} in the URL template. Optional, defaults to xplat.FileExt(). | ||
Ext string | ||
|
||
// OsReplacement maps from a GOOS to the os keyword used for the download. Optional, defaults to empty. | ||
OsReplacement map[string]string | ||
|
||
// ArchReplacement maps from a GOARCH to the arch keyword used for the download. Optional, defaults to empty. | ||
ArchReplacement map[string]string | ||
|
||
// Hook to call after downloading the file. | ||
Hook PostDownloadHook | ||
} | ||
|
||
// DownloadToGopathBin takes a Go templated URL and expands template variables | ||
// - srcTemplate is the URL | ||
// - version is the version to substitute into the template | ||
// - ext is the file extension to substitute into the template | ||
// | ||
// Template Variables: | ||
// - {{.GOOS}} | ||
// - {{.GOARCH}} | ||
// - {{.EXT}} | ||
// - {{.VERSION}} | ||
func DownloadToGopathBin(opts DownloadOptions) error { | ||
src, err := RenderTemplate(opts.UrlTemplate, opts) | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("Downloading %s...", src) | ||
|
||
err = gopath.EnsureGopathBin() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Download to a temp file | ||
tmpDir, err := ioutil.TempDir("", "magex") | ||
if err != nil { | ||
return errors.Wrap(err, "could not create temporary directory") | ||
} | ||
defer os.RemoveAll(tmpDir) | ||
tmpFile := filepath.Join(tmpDir, filepath.Base(src)) | ||
|
||
r, err := http.Get(src) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not resolve %s", src) | ||
} | ||
defer r.Body.Close() | ||
|
||
f, err := os.OpenFile(tmpFile, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not open %s", tmpFile) | ||
} | ||
defer f.Close() | ||
|
||
// Download to the temp file | ||
_, err = io.Copy(f, r.Body) | ||
if err != nil { | ||
errors.Wrapf(err, "error downloading %s", src) | ||
} | ||
f.Close() | ||
|
||
// Call a hook to allow for extracting or modifying the downloaded file | ||
var tmpBin = tmpFile | ||
if opts.Hook != nil { | ||
tmpBin, err = opts.Hook(tmpFile) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Make the binary executable | ||
err = os.Chmod(tmpBin, 0755) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not make %s executable", tmpBin) | ||
} | ||
|
||
// Move it to GOPATH/bin | ||
dest := filepath.Join(gopath.GetGopathBin(), opts.Name+xplat.FileExt()) | ||
err = shx.Copy(tmpBin, dest) | ||
return errors.Wrapf(err, "error copying %s to %s", tmpBin, dest) | ||
} | ||
|
||
// RenderTemplate takes a Go templated string and expands template variables | ||
// Available Template Variables: | ||
// - {{.GOOS}} | ||
// - {{.GOARCH}} | ||
// - {{.EXT}} | ||
// - {{.VERSION}} | ||
func RenderTemplate(tmplContents string, opts DownloadOptions) (string, error) { | ||
tmpl, err := template.New("url").Parse(tmplContents) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "error parsing %s as a Go template", opts.UrlTemplate) | ||
} | ||
|
||
srcData := struct { | ||
GOOS string | ||
GOARCH string | ||
EXT string | ||
VERSION string | ||
}{ | ||
GOOS: runtime.GOOS, | ||
GOARCH: runtime.GOARCH, | ||
EXT: opts.Ext, | ||
VERSION: opts.Version, | ||
} | ||
|
||
if overrideGoos, ok := opts.OsReplacement[runtime.GOOS]; ok { | ||
srcData.GOOS = overrideGoos | ||
} | ||
|
||
if overrideGoarch, ok := opts.ArchReplacement[runtime.GOARCH]; ok { | ||
srcData.GOARCH = overrideGoarch | ||
} | ||
|
||
buf := &bytes.Buffer{} | ||
err = tmpl.Execute(buf, srcData) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "error rendering %s as a Go template with data: %#v", opts.UrlTemplate, srcData) | ||
} | ||
|
||
return buf.String(), nil | ||
} |
Oops, something went wrong.