Add support for downloading source artifacts from Github release

This adds support for retrieving the tarball or zip source artifact from a
Github release. This is done through defining a
"params.include_source_tarball" or "params.include_source_zip" boolean setting.

With this, the resource will download the respective source artfact into the
target directory as either "source.tar.gz" or "source.zip".
This commit is contained in:
Ken Robertson
2015-09-23 11:11:05 -07:00
parent 8508409f8a
commit d563c2714c
5 changed files with 168 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@@ -84,12 +85,34 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
fmt.Fprintf(c.writer, "downloading asset: %s\n", *asset.Name)
err := c.downloadFile(asset, path)
err := c.downloadAsset(asset, path)
if err != nil {
return InResponse{}, err
}
}
if request.Params.IncludeSourceTarball {
u, err := c.github.GetTarballLink(request.Version.Tag)
if err != nil {
return InResponse{}, err
}
fmt.Fprintln(c.writer, "downloading source tarball to source.tar.gz")
if err := c.downloadFile(u.String(), filepath.Join(destDir, "source.tar.gz")); err != nil {
return InResponse{}, err
}
}
if request.Params.IncludeSourceZip {
u, err := c.github.GetZipballLink(request.Version.Tag)
if err != nil {
return InResponse{}, err
}
fmt.Fprintln(c.writer, "downloading source zip to source.zip")
if err := c.downloadFile(u.String(), filepath.Join(destDir, "source.zip")); err != nil {
return InResponse{}, err
}
}
return InResponse{
Version: Version{
Tag: *foundRelease.TagName,
@@ -98,7 +121,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
}, nil
}
func (c *InCommand) downloadFile(asset github.ReleaseAsset, destPath string) error {
func (c *InCommand) downloadAsset(asset github.ReleaseAsset, destPath string) error {
out, err := os.Create(destPath)
if err != nil {
return err
@@ -118,3 +141,28 @@ func (c *InCommand) downloadFile(asset github.ReleaseAsset, destPath string) err
return nil
}
func (c *InCommand) downloadFile(url, destPath string) error {
out, err := os.Create(destPath)
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download file: HTTP status %d: %s", resp.StatusCode, resp.Status)
}
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}