mirror of https://github.com/docker/docs.git
Only update download/untar progress every 1%. Related to #49
This commit is contained in:
parent
2feefb4375
commit
b5c1cd7991
|
@ -110,38 +110,42 @@ func Curl(url string, stderr io.Writer) (io.Reader, error) {
|
||||||
func Download(url string, stderr io.Writer) (*http.Response, error) {
|
func Download(url string, stderr io.Writer) (*http.Response, error) {
|
||||||
var resp *http.Response
|
var resp *http.Response
|
||||||
var err error = nil
|
var err error = nil
|
||||||
|
|
||||||
fmt.Fprintf(stderr, "Download start\n") // FIXME: Replace with progress bar
|
|
||||||
if resp, err = http.Get(url); err != nil {
|
if resp, err = http.Get(url); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if resp.StatusCode >= 400 {
|
if resp.StatusCode >= 400 {
|
||||||
return nil, errors.New("Got HTTP status code >= 400: " + resp.Status)
|
return nil, errors.New("Got HTTP status code >= 400: " + resp.Status)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(stderr, "Download end\n") // FIXME: Replace with progress bar
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reader with progress bar
|
// Reader with progress bar
|
||||||
type progressReader struct {
|
type progressReader struct {
|
||||||
reader io.ReadCloser // Stream to read from
|
reader io.ReadCloser // Stream to read from
|
||||||
output io.Writer // Where to send progress bar to
|
output io.Writer // Where to send progress bar to
|
||||||
read_total int // Expected stream length (bytes)
|
read_total int // Expected stream length (bytes)
|
||||||
read_progress int // How much has been read so far (bytes)
|
read_progress int // How much has been read so far (bytes)
|
||||||
|
last_update int // How many bytes read at least update
|
||||||
}
|
}
|
||||||
func (r *progressReader) Read(p []byte) (n int, err error) {
|
func (r *progressReader) Read(p []byte) (n int, err error) {
|
||||||
read, err := io.ReadCloser(r.reader).Read(p)
|
read, err := io.ReadCloser(r.reader).Read(p)
|
||||||
// FIXME: Don't print progress bar on every read
|
|
||||||
r.read_progress += read
|
r.read_progress += read
|
||||||
fmt.Fprintf(r.output, "%d/%d (%.2f%%)\n",
|
|
||||||
r.read_progress,
|
// Only update progress for every 1% read
|
||||||
r.read_total,
|
update_every := int(0.01 * float64(r.read_total))
|
||||||
float64(r.read_progress) / float64(r.read_total) * 100)
|
if r.read_progress - r.last_update > update_every {
|
||||||
|
fmt.Fprintf(r.output, "%d/%d (%.0f%%)\n",
|
||||||
|
r.read_progress,
|
||||||
|
r.read_total,
|
||||||
|
float64(r.read_progress) / float64(r.read_total) * 100)
|
||||||
|
r.last_update = r.read_progress
|
||||||
|
}
|
||||||
|
|
||||||
return read, err
|
return read, err
|
||||||
}
|
}
|
||||||
func (r *progressReader) Close() error {
|
func (r *progressReader) Close() error {
|
||||||
return io.ReadCloser(r.reader).Close()
|
return io.ReadCloser(r.reader).Close()
|
||||||
}
|
}
|
||||||
func ProgressReader(r io.ReadCloser, size int, output io.Writer) *progressReader {
|
func ProgressReader(r io.ReadCloser, size int, output io.Writer) *progressReader {
|
||||||
return &progressReader{r, output, size, 0}
|
return &progressReader{r, output, size, 0, 0}
|
||||||
}
|
}
|
||||||
|
|
|
@ -432,7 +432,7 @@ func (srv *Server) CmdPull(stdin io.ReadCloser, stdout io.Writer, args ...string
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Fprintf(stdout, "Unpacking to %s\n", name)
|
fmt.Fprintf(stdout, "Downloading and unpacking to %s\n", name)
|
||||||
img, err := srv.images.Import(name, archive, nil)
|
img, err := srv.images.Import(name, archive, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue