2018-09-04 11:57:21 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
2018-09-05 06:50:53 -04:00
|
|
|
"io"
|
2018-09-05 11:15:54 -04:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2018-09-05 06:50:53 -04:00
|
|
|
|
2018-09-04 11:57:21 -04:00
|
|
|
"github.com/cheggaaa/pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProgressBar allows to graphically display
|
|
|
|
// a self refreshing progress bar.
|
|
|
|
type ProgressBar interface {
|
|
|
|
Start(total uint64)
|
2018-09-05 06:50:53 -04:00
|
|
|
Add(current uint64)
|
|
|
|
NewProxyReader(r io.Reader) (proxy io.Reader)
|
2018-09-04 11:57:21 -04:00
|
|
|
Finish()
|
|
|
|
}
|
|
|
|
|
2018-09-05 11:15:54 -04:00
|
|
|
type StackableProgressBar struct {
|
|
|
|
total uint64
|
|
|
|
started bool
|
|
|
|
BasicProgressBar
|
|
|
|
startOnce sync.Once
|
|
|
|
group sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ProgressBar = new(StackableProgressBar)
|
|
|
|
|
|
|
|
func (spb *StackableProgressBar) start() {
|
|
|
|
spb.BasicProgressBar.ProgressBar = pb.New(0)
|
|
|
|
spb.BasicProgressBar.ProgressBar.SetUnits(pb.U_BYTES)
|
|
|
|
|
|
|
|
spb.BasicProgressBar.ProgressBar.Start()
|
|
|
|
go func() {
|
|
|
|
spb.group.Wait()
|
|
|
|
spb.BasicProgressBar.ProgressBar.Finish()
|
|
|
|
spb.startOnce = sync.Once{}
|
|
|
|
spb.BasicProgressBar.ProgressBar = nil
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (spb *StackableProgressBar) Start(total uint64) {
|
|
|
|
atomic.AddUint64(&spb.total, total)
|
|
|
|
spb.group.Add(1)
|
|
|
|
spb.startOnce.Do(spb.start)
|
|
|
|
spb.SetTotal64(int64(spb.total))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (spb *StackableProgressBar) Finish() {
|
|
|
|
spb.group.Done()
|
|
|
|
}
|
|
|
|
|
2018-09-04 11:57:21 -04:00
|
|
|
type BasicProgressBar struct {
|
|
|
|
*pb.ProgressBar
|
|
|
|
}
|
|
|
|
|
2018-09-05 11:15:54 -04:00
|
|
|
var _ ProgressBar = new(BasicProgressBar)
|
|
|
|
|
2018-09-04 11:57:21 -04:00
|
|
|
func (bpb *BasicProgressBar) Start(total uint64) {
|
|
|
|
bpb.SetTotal64(int64(total))
|
|
|
|
bpb.ProgressBar.Start()
|
|
|
|
}
|
|
|
|
|
2018-09-05 06:50:53 -04:00
|
|
|
func (bpb *BasicProgressBar) Add(current uint64) {
|
|
|
|
bpb.ProgressBar.Add64(int64(current))
|
|
|
|
}
|
|
|
|
func (bpb *BasicProgressBar) NewProxyReader(r io.Reader) io.Reader {
|
|
|
|
return &ProxyReader{
|
|
|
|
Reader: r,
|
|
|
|
ProgressBar: bpb,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (bpb *BasicProgressBar) NewProxyReadCloser(r io.ReadCloser) io.ReadCloser {
|
|
|
|
return &ProxyReader{
|
|
|
|
Reader: r,
|
|
|
|
ProgressBar: bpb,
|
|
|
|
}
|
2018-09-04 11:57:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NoopProgressBar is a silent progress bar
|
|
|
|
type NoopProgressBar struct {
|
|
|
|
}
|
|
|
|
|
2018-09-05 11:15:54 -04:00
|
|
|
var _ ProgressBar = new(NoopProgressBar)
|
|
|
|
|
2018-09-05 06:50:53 -04:00
|
|
|
func (npb *NoopProgressBar) Start(uint64) {}
|
|
|
|
func (npb *NoopProgressBar) Add(uint64) {}
|
|
|
|
func (npb *NoopProgressBar) Finish() {}
|
|
|
|
func (npb *NoopProgressBar) NewProxyReader(r io.Reader) io.Reader { return r }
|
|
|
|
func (npb *NoopProgressBar) NewProxyReadCloser(r io.ReadCloser) io.ReadCloser { return r }
|
2018-09-04 11:57:21 -04:00
|
|
|
|
2018-09-05 06:50:53 -04:00
|
|
|
// ProxyReader implements io.ReadCloser but sends
|
|
|
|
// count of read bytes to progress bar
|
|
|
|
type ProxyReader struct {
|
|
|
|
io.Reader
|
|
|
|
ProgressBar
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ProxyReader) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = r.Reader.Read(p)
|
|
|
|
r.ProgressBar.Add(uint64(n))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the reader if it implements io.Closer
|
|
|
|
func (r *ProxyReader) Close() (err error) {
|
|
|
|
if closer, ok := r.Reader.(io.Closer); ok {
|
|
|
|
return closer.Close()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|