Преглед изворни кода

Add Download method for native Go downloading (to replace curl). Catch 404s.
Ref: #49, #50

Charles Hooper пре 12 година
родитељ
комит
49554f47f6
1 измењених фајлова са 20 додато и 0 уклоњено
  1. 20 0
      future/future.go

+ 20 - 0
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
+}