From 49554f47f60568c3df942f274ab45d92f451fbcc Mon Sep 17 00:00:00 2001 From: Charles Hooper Date: Tue, 12 Mar 2013 20:50:11 +0000 Subject: [PATCH] Add Download method for native Go downloading (to replace curl). Catch 404s. Ref: #49, #50 --- future/future.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/future/future.go b/future/future.go index 208484df1a..a3dd9f7efa 100644 --- a/future/future.go +++ b/future/future.go @@ -3,9 +3,11 @@ package future import ( "bytes" "crypto/sha256" + "errors" "fmt" "io" "math/rand" + "net/http" "os/exec" "time" ) @@ -103,3 +105,21 @@ func Curl(url string, stderr io.Writer) (io.Reader, error) { } return output, nil } + +// Request a given URL and return an io.Reader +func Download(url string, stderr io.Writer) (io.Reader, error) { + var resp *http.Response + var archive io.ReadCloser = nil + var err error = nil + + fmt.Fprintf(stderr, "Download start\n") // FIXME: Replace with progress bar + if resp, err = http.Get(url); err != nil { + return nil, err + } + if resp.StatusCode >= 400 { + return nil, errors.New("Got HTTP status code >= 400: " + resp.Status) + } + archive = resp.Body + fmt.Fprintf(stderr, "Download end\n") // FIXME: Replace with progress bar + return archive, nil +}