Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to download artifacts automatically based on a regular expression #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"text/tabwriter"
"time"
Expand Down Expand Up @@ -105,6 +108,35 @@ func (p *Project) String() string {
return fmt.Sprintf("%s/%s", p.Account, p.Repository)
}

// Download 'url' to local file 'filename', creating directories as required
func downloadFile(filename string, url string) (err error) {
if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil {
return err
}
out, err := os.Create(filename)
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("bad status: %s", resp.Status)
}

_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}

return nil
}

func main() {
currentProject := getCurrentProject()

Expand Down Expand Up @@ -153,7 +185,7 @@ func main() {
)
}

baseURL, err := url.Parse(c.String("host") + "/api/v1/")
baseURL, err := url.Parse(c.String("host") + "/api/v1.1/")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to include this change? There is a PR over on go-circleci to switch or add support for the v1.1 API, but just switching the path causes 404s for many endpoints as the v1.1 API requires you to prefix the path with /github or /bitbucket

if err != nil {
return err
}
Expand Down Expand Up @@ -431,6 +463,12 @@ func main() {
Usage: "Show artifacts for specified build num (leave empty for latest)",
EnvVar: "CIRCLE_BUILD_NUM",
},
cli.StringFlag{
Name: "download, d",
Value: "",
Usage: "Download artifacts that match this pattern",
EnvVar: "CIRCLE_DOWNLOAD_PATTERN",
},
},
Action: func(c *cli.Context) {
var buildNum int
Expand Down Expand Up @@ -459,8 +497,24 @@ func main() {

t := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0)
fmt.Fprintf(t, "Node\tPath\tURL\n")
var re *regexp.Regexp
if c.IsSet("download") {
re, err = regexp.Compile(c.String("download"))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
for _, artifact := range artifacts {
fmt.Fprintf(t, "%d\t%s\t%s\n", artifact.NodeIndex, artifact.Path, artifact.URL)
if re != nil {
if re.MatchString(artifact.Path) {
if err := downloadFile(artifact.Path, artifact.URL); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
}
}
t.Flush()
},
Expand Down