From 003ec21d360b08d50b61834795692542fff295e2 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Tue, 26 Feb 2013 17:28:24 -0800 Subject: [PATCH] If curl is installed, 'docker pull' will use it to download images with a pretty progress bar. Otherwise it will fallback to regular http.Get() --- README.md | 2 +- future/future.go | 17 +++++++++++++++++ server/server.go | 12 +++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cf3f74dc61..aea3d10e9d 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ Step by step host setup 3. Type the following commands: apt-get update - apt-get install lxc wget bsdtar + apt-get install lxc wget bsdtar curl 4. Download the latest version of the [docker binaries](https://dl.dropbox.com/u/20637798/docker.tar.gz) (`wget https://dl.dropbox.com/u/20637798/docker.tar.gz`) (warning: this may not be the most up-to-date build) 5. Extract the contents of the tar file `tar -xf docker.tar.gz` diff --git a/future/future.go b/future/future.go index 75caab3d19..33f1f8925c 100644 --- a/future/future.go +++ b/future/future.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "math/rand" + "os/exec" "time" ) @@ -83,3 +84,19 @@ func Pv(src io.Reader, info io.Writer) io.Reader { }() return r } + +// Curl makes an http request by executing the unix command 'curl', and returns +// the body of the response. If `stderr` is not nil, a progress bar will be +// written to it. +func Curl(url string, stderr io.Writer) (io.Reader, error) { + curl := exec.Command("curl", "-#", "-L", url) + output, err := curl.StdoutPipe() + if err != nil { + return nil, err + } + curl.Stderr = stderr + if err := curl.Start(); err != nil { + return nil, err + } + return output, nil +} diff --git a/server/server.go b/server/server.go index ee2c997634..2ef42f8b16 100644 --- a/server/server.go +++ b/server/server.go @@ -390,12 +390,18 @@ func (srv *Server) CmdPull(stdin io.ReadCloser, stdout io.Writer, args ...string u.Path = path.Join("/docker.io/images", u.Path) } fmt.Fprintf(stdout, "Downloading from %s\n", u.String()) - resp, err := http.Get(u.String()) + // Download with curl (pretty progress bar) + // If curl is not available, fallback to http.Get() + archive, err := future.Curl(u.String(), stdout) if err != nil { - return err + if resp, err := http.Get(u.String()); err != nil { + return err + } else { + archive = resp.Body + } } fmt.Fprintf(stdout, "Unpacking to %s\n", name) - img, err := srv.images.Import(name, resp.Body, nil) + img, err := srv.images.Import(name, archive, nil) if err != nil { return err }