... | ... |
@@ -383,11 +383,11 @@ func (srv *Server) CmdImport(stdin io.ReadCloser, stdout io.Writer, args ...stri |
383 | 383 |
fmt.Fprintf(stdout, "Downloading from %s\n", u.String()) |
384 | 384 |
// Download with curl (pretty progress bar) |
385 | 385 |
// If curl is not available, fallback to http.Get() |
386 |
- resp, err = future.Download(u.String(), stdout) |
|
386 |
+ resp, err = Download(u.String(), stdout) |
|
387 | 387 |
if err != nil { |
388 | 388 |
return err |
389 | 389 |
} |
390 |
- archive = future.ProgressReader(resp.Body, int(resp.ContentLength), stdout) |
|
390 |
+ archive = ProgressReader(resp.Body, int(resp.ContentLength), stdout) |
|
391 | 391 |
} |
392 | 392 |
fmt.Fprintf(stdout, "Unpacking to %s\n", name) |
393 | 393 |
img, err := srv.runtime.graph.Create(archive, "", "") |
... | ... |
@@ -3,11 +3,9 @@ package future |
3 | 3 |
import ( |
4 | 4 |
"bytes" |
5 | 5 |
"crypto/sha256" |
6 |
- "errors" |
|
7 | 6 |
"fmt" |
8 | 7 |
"io" |
9 | 8 |
"math/rand" |
10 |
- "net/http" |
|
11 | 9 |
"time" |
12 | 10 |
) |
13 | 11 |
|
... | ... |
@@ -62,52 +60,3 @@ func Pv(src io.Reader, info io.Writer) io.Reader { |
62 | 62 |
}() |
63 | 63 |
return r |
64 | 64 |
} |
65 |
- |
|
66 |
-// Request a given URL and return an io.Reader |
|
67 |
-func Download(url string, stderr io.Writer) (*http.Response, error) { |
|
68 |
- var resp *http.Response |
|
69 |
- var err error = nil |
|
70 |
- if resp, err = http.Get(url); err != nil { |
|
71 |
- return nil, err |
|
72 |
- } |
|
73 |
- if resp.StatusCode >= 400 { |
|
74 |
- return nil, errors.New("Got HTTP status code >= 400: " + resp.Status) |
|
75 |
- } |
|
76 |
- return resp, nil |
|
77 |
-} |
|
78 |
- |
|
79 |
-// Reader with progress bar |
|
80 |
-type progressReader struct { |
|
81 |
- reader io.ReadCloser // Stream to read from |
|
82 |
- output io.Writer // Where to send progress bar to |
|
83 |
- read_total int // Expected stream length (bytes) |
|
84 |
- read_progress int // How much has been read so far (bytes) |
|
85 |
- last_update int // How many bytes read at least update |
|
86 |
-} |
|
87 |
- |
|
88 |
-func (r *progressReader) Read(p []byte) (n int, err error) { |
|
89 |
- read, err := io.ReadCloser(r.reader).Read(p) |
|
90 |
- r.read_progress += read |
|
91 |
- |
|
92 |
- // Only update progress for every 1% read |
|
93 |
- update_every := int(0.01 * float64(r.read_total)) |
|
94 |
- if r.read_progress-r.last_update > update_every || r.read_progress == r.read_total { |
|
95 |
- fmt.Fprintf(r.output, "%d/%d (%.0f%%)\r", |
|
96 |
- r.read_progress, |
|
97 |
- r.read_total, |
|
98 |
- float64(r.read_progress)/float64(r.read_total)*100) |
|
99 |
- r.last_update = r.read_progress |
|
100 |
- } |
|
101 |
- // Send newline when complete |
|
102 |
- if err == io.EOF { |
|
103 |
- fmt.Fprintf(r.output, "\n") |
|
104 |
- } |
|
105 |
- |
|
106 |
- return read, err |
|
107 |
-} |
|
108 |
-func (r *progressReader) Close() error { |
|
109 |
- return io.ReadCloser(r.reader).Close() |
|
110 |
-} |
|
111 |
-func ProgressReader(r io.ReadCloser, size int, output io.Writer) *progressReader { |
|
112 |
- return &progressReader{r, output, size, 0, 0} |
|
113 |
-} |
... | ... |
@@ -3,8 +3,10 @@ package docker |
3 | 3 |
import ( |
4 | 4 |
"bytes" |
5 | 5 |
"container/list" |
6 |
+ "errors" |
|
6 | 7 |
"fmt" |
7 | 8 |
"io" |
9 |
+ "net/http" |
|
8 | 10 |
"os" |
9 | 11 |
"os/exec" |
10 | 12 |
"path/filepath" |
... | ... |
@@ -12,6 +14,55 @@ import ( |
12 | 12 |
"time" |
13 | 13 |
) |
14 | 14 |
|
15 |
+// Request a given URL and return an io.Reader |
|
16 |
+func Download(url string, stderr io.Writer) (*http.Response, error) { |
|
17 |
+ var resp *http.Response |
|
18 |
+ var err error = nil |
|
19 |
+ if resp, err = http.Get(url); err != nil { |
|
20 |
+ return nil, err |
|
21 |
+ } |
|
22 |
+ if resp.StatusCode >= 400 { |
|
23 |
+ return nil, errors.New("Got HTTP status code >= 400: " + resp.Status) |
|
24 |
+ } |
|
25 |
+ return resp, nil |
|
26 |
+} |
|
27 |
+ |
|
28 |
+// Reader with progress bar |
|
29 |
+type progressReader struct { |
|
30 |
+ reader io.ReadCloser // Stream to read from |
|
31 |
+ output io.Writer // Where to send progress bar to |
|
32 |
+ read_total int // Expected stream length (bytes) |
|
33 |
+ read_progress int // How much has been read so far (bytes) |
|
34 |
+ last_update int // How many bytes read at least update |
|
35 |
+} |
|
36 |
+ |
|
37 |
+func (r *progressReader) Read(p []byte) (n int, err error) { |
|
38 |
+ read, err := io.ReadCloser(r.reader).Read(p) |
|
39 |
+ r.read_progress += read |
|
40 |
+ |
|
41 |
+ // Only update progress for every 1% read |
|
42 |
+ update_every := int(0.01 * float64(r.read_total)) |
|
43 |
+ if r.read_progress-r.last_update > update_every || r.read_progress == r.read_total { |
|
44 |
+ fmt.Fprintf(r.output, "%d/%d (%.0f%%)\r", |
|
45 |
+ r.read_progress, |
|
46 |
+ r.read_total, |
|
47 |
+ float64(r.read_progress)/float64(r.read_total)*100) |
|
48 |
+ r.last_update = r.read_progress |
|
49 |
+ } |
|
50 |
+ // Send newline when complete |
|
51 |
+ if err == io.EOF { |
|
52 |
+ fmt.Fprintf(r.output, "\n") |
|
53 |
+ } |
|
54 |
+ |
|
55 |
+ return read, err |
|
56 |
+} |
|
57 |
+func (r *progressReader) Close() error { |
|
58 |
+ return io.ReadCloser(r.reader).Close() |
|
59 |
+} |
|
60 |
+func ProgressReader(r io.ReadCloser, size int, output io.Writer) *progressReader { |
|
61 |
+ return &progressReader{r, output, size, 0, 0} |
|
62 |
+} |
|
63 |
+ |
|
15 | 64 |
// HumanDuration returns a human-readable approximation of a duration |
16 | 65 |
// (eg. "About a minute", "4 hours ago", etc.) |
17 | 66 |
func HumanDuration(d time.Duration) string { |