implement /in

fetches a given list of globs from the assets.

also be durable to 'v' prefixing version tags

Signed-off-by: Alex Suraci <asuraci@pivotal.io>
This commit is contained in:
Chris Brown
2015-02-17 18:03:42 -08:00
committed by Alex Suraci
parent 7e47c2696e
commit efbfb91683
10 changed files with 466 additions and 14 deletions

116
in_command.go Normal file
View File

@@ -0,0 +1,116 @@
package resource
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"sort"
"github.com/google/go-github/github"
)
type InCommand struct {
github GitHub
}
func NewInCommand(github GitHub) *InCommand {
return &InCommand{
github: github,
}
}
func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
releases, err := c.github.ListReleases()
if err != nil {
return InResponse{}, nil
}
sort.Sort(byVersion(releases))
if len(releases) == 0 {
return InResponse{}, errors.New("no releases")
}
var foundRelease *github.RepositoryRelease
if request.Version == nil {
foundRelease = &releases[len(releases)-1]
} else {
for _, release := range releases {
if *release.TagName == request.Version.Tag {
foundRelease = &release
break
}
}
}
if foundRelease == nil {
return InResponse{}, fmt.Errorf("could not find release with tag: %s", request.Version.Tag)
}
assets, err := c.github.ListReleaseAssets(foundRelease)
if err != nil {
return InResponse{}, nil
}
for _, asset := range assets {
url := *asset.BrowserDownloadURL
path := filepath.Join(destDir, *asset.Name)
var matchFound bool
if len(request.Params.Globs) == 0 {
matchFound = true
} else {
for _, glob := range request.Params.Globs {
matches, err := filepath.Match(glob, *asset.Name)
if err != nil {
return InResponse{}, err
}
if matches {
matchFound = true
break
}
}
}
if !matchFound {
continue
}
err := c.downloadFile(url, path)
if err != nil {
return InResponse{}, nil
}
}
return InResponse{
Version: Version{
Tag: *foundRelease.TagName,
},
}, 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()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}