From 7fa4e054f3f6b312572598f5dde0c681dfd6844b Mon Sep 17 00:00:00 2001 From: Pranav Jituri Date: Fri, 9 Aug 2019 04:02:29 +0530 Subject: [PATCH 1/2] Fixed the progress bars ^_^ --- go.mod | 3 +-- go.sum | 4 ++++ pkg/util/progressbar.go | 42 +++++++++++++---------------------------- 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/go.mod b/go.mod index 39ca21b95d27..702444e2f030 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/blang/semver v3.5.0+incompatible github.com/c4milo/gotoolkit v0.0.0-20170318115440-bcc06269efa9 // indirect github.com/cenkalti/backoff v2.2.1+incompatible - github.com/cheggaaa/pb v1.0.27 + github.com/cheggaaa/pb/v3 v3.0.1 github.com/cloudfoundry-attic/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21 github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21 // indirect github.com/docker/docker v1.13.1 // indirect @@ -35,7 +35,6 @@ require ( github.com/johanneswuerbach/nfsexports v0.0.0-20181204082207-1aa528dcb345 github.com/libvirt/libvirt-go v3.4.0+incompatible github.com/machine-drivers/docker-machine-driver-vmware v0.1.1 - github.com/mattn/go-colorable v0.1.2 // indirect github.com/mattn/go-isatty v0.0.8 github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936 github.com/moby/hyperkit v0.0.0-20171020124204-a12cd7250bcd diff --git a/go.sum b/go.sum index d984677ef817..9c455b2b7ee4 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,8 @@ github.com/Sirupsen/logrus v0.0.0-20170822132746-89742aefa4b2 h1:k1A7eIeUk6rnX2y github.com/Sirupsen/logrus v0.0.0-20170822132746-89742aefa4b2/go.mod h1:rmk17hk6i8ZSAJkSDa7nOxamrG+SP4P0mm+DAvExv4U= github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk= github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= +github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM= +github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= @@ -59,6 +61,8 @@ github.com/cespare/prettybench v0.0.0-20150116022406-03b8cfe5406c/go.mod h1:Xe6Z github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5/go.mod h1:/iP1qXHoty45bqomnu2LM+VVyAEdWN+vtSHGlQgyxbw= github.com/cheggaaa/pb v1.0.27 h1:wIkZHkNfC7R6GI5w7l/PdAdzXzlrbcI3p8OAlnkTsnc= github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s= +github.com/cheggaaa/pb/v3 v3.0.1 h1:m0BngUk2LuSRYdx4fujDKNRXNDpbNCfptPfVT2m6OJY= +github.com/cheggaaa/pb/v3 v3.0.1/go.mod h1:SqqeMF/pMOIu3xgGoxtPYhMNQP258xE4x/XRTYua+KU= github.com/client9/misspell v0.0.0-20170928000206-9ce5d979ffda/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cloudflare/cfssl v0.0.0-20180726162950-56268a613adf/go.mod h1:yMWuSON2oQp+43nFtAV/uvKQIFpSPerB57DCt9t8sSA= diff --git a/pkg/util/progressbar.go b/pkg/util/progressbar.go index e1bb6ebb703e..98826d0225ce 100644 --- a/pkg/util/progressbar.go +++ b/pkg/util/progressbar.go @@ -21,22 +21,19 @@ limitations under the License. package util import ( - "fmt" "io" "path/filepath" "sync" - "github.com/cheggaaa/pb" - "github.com/golang/glog" + "github.com/cheggaaa/pb/v3" "github.com/hashicorp/go-getter" ) var defaultProgressBar getter.ProgressTracker = &progressBar{} type progressBar struct { - lock sync.Mutex - pool *pb.Pool - pbs int + lock sync.Mutex + progress *pb.ProgressBar } // TrackProgress instantiates a new progress bar that will @@ -45,38 +42,25 @@ type progressBar struct { func (cpb *progressBar) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser { cpb.lock.Lock() defer cpb.lock.Unlock() - - if cpb.pool == nil { - cpb.pool = pb.NewPool() - if err := cpb.pool.Start(); err != nil { - glog.Errorf("pool start: %v", err) - } + if cpb.progress == nil { + cpb.progress = pb.New64(totalSize) } + tmpl := `{{ string . "filePath" }} {{ bar . "-" "█" (cycle . "↖" "↗" "↘" "↙" ) "." "-"}} {{speed . }} {{percent .}} {{ rtime . " ETA - %s"}} {{ etime . " Elapsed - %s"}}` + p := pb.ProgressBarTemplate(tmpl).Start64(totalSize) + p.Set("filePath", filepath.Base(src)) + p.SetCurrent(currentSize) + p.Set(pb.Bytes, true) - p := pb.New64(totalSize) - p.Set64(currentSize) - p.SetUnits(pb.U_BYTES) - p.Prefix(fmt.Sprintf(" %s:", filepath.Base(src))) // Just a hair less than 80 (standard terminal width) for aesthetics & pasting into docs - p.SetWidth(78) - cpb.pool.Add(p) - reader := p.NewProxyReader(stream) + p.SetWidth(79) + barReader := p.NewProxyReader(stream) - cpb.pbs++ return &readCloser{ - Reader: reader, + Reader: barReader, close: func() error { cpb.lock.Lock() defer cpb.lock.Unlock() - p.Finish() - cpb.pbs-- - if cpb.pbs <= 0 { - if err := cpb.pool.Stop(); err != nil { - glog.Errorf("pool stop: %v", err) - } - cpb.pool = nil - } return nil }, } From 42f5b11fb383d402c04d09e875a5d9977756bc8c Mon Sep 17 00:00:00 2001 From: Pranav Jituri Date: Sat, 10 Aug 2019 12:48:49 +0530 Subject: [PATCH 2/2] Fixed as per review comments --- pkg/util/progressbar.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/util/progressbar.go b/pkg/util/progressbar.go index 98826d0225ce..14bb33f169bc 100644 --- a/pkg/util/progressbar.go +++ b/pkg/util/progressbar.go @@ -45,9 +45,8 @@ func (cpb *progressBar) TrackProgress(src string, currentSize, totalSize int64, if cpb.progress == nil { cpb.progress = pb.New64(totalSize) } - tmpl := `{{ string . "filePath" }} {{ bar . "-" "█" (cycle . "↖" "↗" "↘" "↙" ) "." "-"}} {{speed . }} {{percent .}} {{ rtime . " ETA - %s"}} {{ etime . " Elapsed - %s"}}` - p := pb.ProgressBarTemplate(tmpl).Start64(totalSize) - p.Set("filePath", filepath.Base(src)) + p := pb.Full.Start64(totalSize) + p.Set("prefix", filepath.Base(src+": ")) p.SetCurrent(currentSize) p.Set(pb.Bytes, true)