Add Download method for native Go downloading (to replace curl). Catch 404s.

Ref: #49, #50
This commit is contained in:
Charles Hooper 2013-03-12 20:50:11 +00:00
Родитель 8e0986caec
Коммит 49554f47f6
1 изменённых файлов: 20 добавлений и 0 удалений

Просмотреть файл

@ -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
}