Moved Download() and progressReader{} to the main package
This commit is contained in:
parent
299d0b2720
commit
d7c5d060c4
|
@ -383,11 +383,11 @@ func (srv *Server) CmdImport(stdin io.ReadCloser, stdout io.Writer, args ...stri
|
||||||
fmt.Fprintf(stdout, "Downloading from %s\n", u.String())
|
fmt.Fprintf(stdout, "Downloading from %s\n", u.String())
|
||||||
// Download with curl (pretty progress bar)
|
// Download with curl (pretty progress bar)
|
||||||
// If curl is not available, fallback to http.Get()
|
// If curl is not available, fallback to http.Get()
|
||||||
resp, err = future.Download(u.String(), stdout)
|
resp, err = Download(u.String(), stdout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
archive = future.ProgressReader(resp.Body, int(resp.ContentLength), stdout)
|
archive = ProgressReader(resp.Body, int(resp.ContentLength), stdout)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(stdout, "Unpacking to %s\n", name)
|
fmt.Fprintf(stdout, "Unpacking to %s\n", name)
|
||||||
img, err := srv.runtime.graph.Create(archive, "", "")
|
img, err := srv.runtime.graph.Create(archive, "", "")
|
||||||
|
|
|
@ -3,11 +3,9 @@ package future
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -62,52 +60,3 @@ func Pv(src io.Reader, info io.Writer) io.Reader {
|
||||||
}()
|
}()
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request a given URL and return an io.Reader
|
|
||||||
func Download(url string, stderr io.Writer) (*http.Response, error) {
|
|
||||||
var resp *http.Response
|
|
||||||
var err error = nil
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reader with progress bar
|
|
||||||
type progressReader struct {
|
|
||||||
reader io.ReadCloser // Stream to read from
|
|
||||||
output io.Writer // Where to send progress bar to
|
|
||||||
read_total int // Expected stream length (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) {
|
|
||||||
read, err := io.ReadCloser(r.reader).Read(p)
|
|
||||||
r.read_progress += read
|
|
||||||
|
|
||||||
// Only update progress for every 1% read
|
|
||||||
update_every := int(0.01 * float64(r.read_total))
|
|
||||||
if r.read_progress-r.last_update > update_every || r.read_progress == r.read_total {
|
|
||||||
fmt.Fprintf(r.output, "%d/%d (%.0f%%)\r",
|
|
||||||
r.read_progress,
|
|
||||||
r.read_total,
|
|
||||||
float64(r.read_progress)/float64(r.read_total)*100)
|
|
||||||
r.last_update = r.read_progress
|
|
||||||
}
|
|
||||||
// Send newline when complete
|
|
||||||
if err == io.EOF {
|
|
||||||
fmt.Fprintf(r.output, "\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
return read, err
|
|
||||||
}
|
|
||||||
func (r *progressReader) Close() error {
|
|
||||||
return io.ReadCloser(r.reader).Close()
|
|
||||||
}
|
|
||||||
func ProgressReader(r io.ReadCloser, size int, output io.Writer) *progressReader {
|
|
||||||
return &progressReader{r, output, size, 0, 0}
|
|
||||||
}
|
|
||||||
|
|
51
utils.go
51
utils.go
|
@ -3,8 +3,10 @@ package docker
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"container/list"
|
"container/list"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -12,6 +14,55 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Request a given URL and return an io.Reader
|
||||||
|
func Download(url string, stderr io.Writer) (*http.Response, error) {
|
||||||
|
var resp *http.Response
|
||||||
|
var err error = nil
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reader with progress bar
|
||||||
|
type progressReader struct {
|
||||||
|
reader io.ReadCloser // Stream to read from
|
||||||
|
output io.Writer // Where to send progress bar to
|
||||||
|
read_total int // Expected stream length (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) {
|
||||||
|
read, err := io.ReadCloser(r.reader).Read(p)
|
||||||
|
r.read_progress += read
|
||||||
|
|
||||||
|
// Only update progress for every 1% read
|
||||||
|
update_every := int(0.01 * float64(r.read_total))
|
||||||
|
if r.read_progress-r.last_update > update_every || r.read_progress == r.read_total {
|
||||||
|
fmt.Fprintf(r.output, "%d/%d (%.0f%%)\r",
|
||||||
|
r.read_progress,
|
||||||
|
r.read_total,
|
||||||
|
float64(r.read_progress)/float64(r.read_total)*100)
|
||||||
|
r.last_update = r.read_progress
|
||||||
|
}
|
||||||
|
// Send newline when complete
|
||||||
|
if err == io.EOF {
|
||||||
|
fmt.Fprintf(r.output, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return read, err
|
||||||
|
}
|
||||||
|
func (r *progressReader) Close() error {
|
||||||
|
return io.ReadCloser(r.reader).Close()
|
||||||
|
}
|
||||||
|
func ProgressReader(r io.ReadCloser, size int, output io.Writer) *progressReader {
|
||||||
|
return &progressReader{r, output, size, 0, 0}
|
||||||
|
}
|
||||||
|
|
||||||
// HumanDuration returns a human-readable approximation of a duration
|
// HumanDuration returns a human-readable approximation of a duration
|
||||||
// (eg. "About a minute", "4 hours ago", etc.)
|
// (eg. "About a minute", "4 hours ago", etc.)
|
||||||
func HumanDuration(d time.Duration) string {
|
func HumanDuration(d time.Duration) string {
|
||||||
|
|
Loading…
Reference in New Issue