test output resource

This commit is contained in:
Chris Brown
2015-01-31 23:53:38 +00:00
parent f1b2ea069d
commit 793c8a5019
4 changed files with 231 additions and 24 deletions

View File

@@ -1,8 +1,10 @@
package resource
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/google/go-github/github"
)
@@ -20,26 +22,68 @@ func NewOutCommand(github GitHub) *OutCommand {
func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, error) {
params := request.Params
release := &github.RepositoryRelease{}
name, err := c.fileContents(filepath.Join(sourceDir, request.Params.NamePath))
if err != nil {
return OutResponse{}, err
}
tag, err := c.fileContents(filepath.Join(sourceDir, request.Params.TagPath))
if err != nil {
return OutResponse{}, err
}
body := ""
if request.Params.BodyPath != "" {
body, err = c.fileContents(filepath.Join(sourceDir, request.Params.BodyPath))
if err != nil {
return OutResponse{}, err
}
}
release := &github.RepositoryRelease{
Name: github.String(name),
TagName: github.String(tag),
Body: github.String(body),
}
createdRelease, err := c.github.CreateRelease(release)
if err != nil {
return OutResponse{}, err
}
for _, filePath := range params.Globs {
file, err := os.Open(filePath)
for _, fileGlob := range params.Globs {
matches, err := filepath.Glob(filepath.Join(sourceDir, fileGlob))
if err != nil {
return OutResponse{}, err
}
name := filepath.Base(filePath)
err = c.github.UploadReleaseAsset(createdRelease, name, file)
if err != nil {
return OutResponse{}, err
}
for _, filePath := range matches {
file, err := os.Open(filePath)
if err != nil {
return OutResponse{}, err
}
file.Close()
name := filepath.Base(filePath)
err = c.github.UploadReleaseAsset(createdRelease, name, file)
if err != nil {
return OutResponse{}, err
}
file.Close()
}
}
return OutResponse{}, nil
return OutResponse{
Version: Version{
Tag: tag,
},
}, nil
}
func (c *OutCommand) fileContents(path string) (string, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return strings.TrimSpace(string(contents)), nil
}