mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Add Download method for native Go downloading (to replace curl). Catch 404s.
Ref: #49, #50
This commit is contained in:
parent
8e0986caec
commit
49554f47f6
1 changed files with 20 additions and 0 deletions
|
@ -3,9 +3,11 @@ package future
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"net/http"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -103,3 +105,21 @@ func Curl(url string, stderr io.Writer) (io.Reader, error) {
|
||||||
}
|
}
|
||||||
return output, nil
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue