From 153a17b41920bc90629350acdbc00fc33e827edf Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Thu, 30 Apr 2015 18:42:55 -0400 Subject: [PATCH] rkt: progress bar when Content-Length is unknown --- Godeps/Godeps.json | 8 ++++---- .../{mitchellh => coreos}/ioprogress/LICENSE | 0 .../{mitchellh => coreos}/ioprogress/README.md | 0 .../{mitchellh => coreos}/ioprogress/draw.go | 9 +++++---- .../ioprogress/draw_test.go | 4 ++-- .../{mitchellh => coreos}/ioprogress/reader.go | 17 ++++++++--------- .../ioprogress/reader_test.go | 2 +- rkt/images.go | 10 +++++++++- 8 files changed, 29 insertions(+), 21 deletions(-) rename Godeps/_workspace/src/github.com/{mitchellh => coreos}/ioprogress/LICENSE (100%) rename Godeps/_workspace/src/github.com/{mitchellh => coreos}/ioprogress/README.md (100%) rename Godeps/_workspace/src/github.com/{mitchellh => coreos}/ioprogress/draw.go (91%) rename Godeps/_workspace/src/github.com/{mitchellh => coreos}/ioprogress/draw_test.go (97%) rename Godeps/_workspace/src/github.com/{mitchellh => coreos}/ioprogress/reader.go (90%) rename Godeps/_workspace/src/github.com/{mitchellh => coreos}/ioprogress/reader_test.go (95%) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index e0ab2df..6cfd362 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -106,6 +106,10 @@ "Comment": "v2-41-g415a79c4bfa3", "Rev": "415a79c4bfa377b484b16c632347b3925a0f35ff" }, + { + "ImportPath": "github.com/coreos/ioprogress", + "Rev": "326b3ee6276a33c8bdcf480ca9c99228942ab970" + }, { "ImportPath": "github.com/cznic/b", "Rev": "e2e747ce049fb910cff6b1fd7ad8faf3900939d5" @@ -164,10 +168,6 @@ "Comment": "release.r56-25-g05017fc", "Rev": "05017fcccf23c823bfdea560dcc958a136e54fb7" }, - { - "ImportPath": "github.com/mitchellh/ioprogress", - "Rev": "c2b2845d63abaf335647a8b9c7437e6c11a2a613" - }, { "ImportPath": "github.com/petar/GoLLRB/llrb", "Rev": "53be0d36a84c2a886ca057d34b6aa4468df9ccb4" diff --git a/Godeps/_workspace/src/github.com/mitchellh/ioprogress/LICENSE b/Godeps/_workspace/src/github.com/coreos/ioprogress/LICENSE similarity index 100% rename from Godeps/_workspace/src/github.com/mitchellh/ioprogress/LICENSE rename to Godeps/_workspace/src/github.com/coreos/ioprogress/LICENSE diff --git a/Godeps/_workspace/src/github.com/mitchellh/ioprogress/README.md b/Godeps/_workspace/src/github.com/coreos/ioprogress/README.md similarity index 100% rename from Godeps/_workspace/src/github.com/mitchellh/ioprogress/README.md rename to Godeps/_workspace/src/github.com/coreos/ioprogress/README.md diff --git a/Godeps/_workspace/src/github.com/mitchellh/ioprogress/draw.go b/Godeps/_workspace/src/github.com/coreos/ioprogress/draw.go similarity index 91% rename from Godeps/_workspace/src/github.com/mitchellh/ioprogress/draw.go rename to Godeps/_workspace/src/github.com/coreos/ioprogress/draw.go index 1e6196a..2a0a938 100644 --- a/Godeps/_workspace/src/github.com/mitchellh/ioprogress/draw.go +++ b/Godeps/_workspace/src/github.com/coreos/ioprogress/draw.go @@ -34,7 +34,7 @@ func DrawTerminalf(w io.Writer, f DrawTextFormatFunc) DrawFunc { var maxLength int return func(progress, total int64) error { - if progress == -1 || total == -1 { + if progress == -1 && total == -1 { _, err := fmt.Fprintf(w, "\n") return err } @@ -60,7 +60,7 @@ var byteUnits = []string{"B", "KB", "MB", "GB", "TB", "PB"} // DrawTextFormatBytes is a DrawTextFormatFunc that formats the progress // and total into human-friendly byte formats. func DrawTextFormatBytes(progress, total int64) string { - return fmt.Sprintf("%s/%s", byteUnitStr(progress), byteUnitStr(total)) + return fmt.Sprintf("%s/%s", ByteUnitStr(progress), ByteUnitStr(total)) } // DrawTextFormatBar returns a DrawTextFormatFunc that draws a progress @@ -84,11 +84,12 @@ func DrawTextFormatBar(width int64) DrawTextFormatFunc { return fmt.Sprintf( "[%s%s]", strings.Repeat("=", int(current)), - strings.Repeat(" ", int(width - current))) + strings.Repeat(" ", int(width-current))) } } -func byteUnitStr(n int64) string { +// ByteUnitStr pretty prints a number of bytes. +func ByteUnitStr(n int64) string { var unit string size := float64(n) for i := 1; i < len(byteUnits); i++ { diff --git a/Godeps/_workspace/src/github.com/mitchellh/ioprogress/draw_test.go b/Godeps/_workspace/src/github.com/coreos/ioprogress/draw_test.go similarity index 97% rename from Godeps/_workspace/src/github.com/mitchellh/ioprogress/draw_test.go rename to Godeps/_workspace/src/github.com/coreos/ioprogress/draw_test.go index e26b52c..0a83d63 100644 --- a/Godeps/_workspace/src/github.com/mitchellh/ioprogress/draw_test.go +++ b/Godeps/_workspace/src/github.com/coreos/ioprogress/draw_test.go @@ -47,8 +47,8 @@ func TestDrawTextFormatBar(t *testing.T) { } func TestDrawTextFormatBytes(t *testing.T) { - cases := []struct{ - P, T int64 + cases := []struct { + P, T int64 Output string }{ { diff --git a/Godeps/_workspace/src/github.com/mitchellh/ioprogress/reader.go b/Godeps/_workspace/src/github.com/coreos/ioprogress/reader.go similarity index 90% rename from Godeps/_workspace/src/github.com/mitchellh/ioprogress/reader.go rename to Godeps/_workspace/src/github.com/coreos/ioprogress/reader.go index 5d19086..7d52731 100644 --- a/Godeps/_workspace/src/github.com/mitchellh/ioprogress/reader.go +++ b/Godeps/_workspace/src/github.com/coreos/ioprogress/reader.go @@ -80,16 +80,15 @@ func (r *Reader) drawProgress() { } func (r *Reader) finishProgress() { - // Only output the final draw if we drawed prior - if !r.lastDraw.IsZero() { - f := r.drawFunc() - f(r.progress, r.Size) - f(-1, -1) + f := r.drawFunc() + f(r.progress, r.Size) - // Reset lastDraw so we don't finish again - var zeroDraw time.Time - r.lastDraw = zeroDraw - } + // Print a newline + f(-1, -1) + + // Reset lastDraw so we don't finish again + var zeroDraw time.Time + r.lastDraw = zeroDraw } func (r *Reader) initProgress() { diff --git a/Godeps/_workspace/src/github.com/mitchellh/ioprogress/reader_test.go b/Godeps/_workspace/src/github.com/coreos/ioprogress/reader_test.go similarity index 95% rename from Godeps/_workspace/src/github.com/mitchellh/ioprogress/reader_test.go rename to Godeps/_workspace/src/github.com/coreos/ioprogress/reader_test.go index ddfb032..a131fe2 100644 --- a/Godeps/_workspace/src/github.com/mitchellh/ioprogress/reader_test.go +++ b/Godeps/_workspace/src/github.com/coreos/ioprogress/reader_test.go @@ -31,7 +31,7 @@ func TestReader(t *testing.T) { } } -const drawReaderStr = "0/6\r2/6\r4/6\r6/6\r\n" +const drawReaderStr = "0/6\r2/6\r4/6\r6/6\r6/6\r\n" // testReader is a test structure to help with testing the Reader by // returning fixed slices of data. diff --git a/rkt/images.go b/rkt/images.go index 8f64744..91f5fd3 100644 --- a/rkt/images.go +++ b/rkt/images.go @@ -32,7 +32,7 @@ import ( "github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/aci" "github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/discovery" "github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/schema/types" - "github.com/coreos/rkt/Godeps/_workspace/src/github.com/mitchellh/ioprogress" + "github.com/coreos/rkt/Godeps/_workspace/src/github.com/coreos/ioprogress" "github.com/coreos/rkt/Godeps/_workspace/src/golang.org/x/crypto/openpgp" "github.com/coreos/rkt/common/apps" "github.com/coreos/rkt/pkg/keystore" @@ -478,6 +478,14 @@ func (f *fetcher) downloadHTTP(url, label string, out writeSyncer) error { barSize := int64(80 - len(prefix) - fmtBytesSize) bar := ioprogress.DrawTextFormatBar(barSize) fmtfunc := func(progress, total int64) string { + // Content-Length is set to -1 when unknown. + if total == -1 { + return fmt.Sprintf( + "%s: %v of an unknown total size", + prefix, + ioprogress.ByteUnitStr(progress), + ) + } return fmt.Sprintf( "%s: %s %s", prefix,