packer-cn/packer/progressbar.go

138 lines
3.0 KiB
Go
Raw Normal View History

package packer
import (
"io"
"sync"
"github.com/cheggaaa/pb"
)
// ProgressBar allows to graphically display
// a self refreshing progress bar.
type ProgressBar interface {
Start(total int64)
Add(current int64)
NewProxyReader(r io.Reader) (proxy io.Reader)
Finish()
}
2018-09-12 19:21:58 -04:00
// StackableProgressBar is a progress bar pool that
// allows to track multiple advencments at once,
// by displaying multiple bars.
type StackableProgressBar struct {
mtx sync.Mutex // locks in Start & Finish
2018-09-12 19:21:58 -04:00
pool *pb.Pool
bars []*BasicProgressBar
2018-09-12 19:21:58 -04:00
wg sync.WaitGroup
}
2018-09-12 19:21:58 -04:00
func (spb *StackableProgressBar) cleanup() {
spb.wg.Wait()
2018-09-12 19:21:58 -04:00
spb.mtx.Lock()
defer spb.mtx.Unlock()
2018-09-12 19:21:58 -04:00
spb.pool.Stop()
spb.pool = nil
spb.bars = nil
}
2018-09-12 19:21:58 -04:00
func (spb *StackableProgressBar) New(identifier string) ProgressBar {
spb.mtx.Lock()
2018-09-12 19:21:58 -04:00
spb.wg.Add(1)
2018-09-12 13:53:15 -04:00
defer spb.mtx.Unlock()
2018-09-12 19:21:58 -04:00
if spb.pool == nil {
spb.pool = pb.NewPool()
go spb.cleanup()
}
2018-09-12 19:21:58 -04:00
bar := NewProgressBar(identifier)
bar.Prefix(identifier)
bar.finishCb = spb.wg.Done
spb.bars = append(spb.bars, bar)
spb.pool.Add(bar.ProgressBar)
spb.pool.Start()
return bar
}
2018-09-06 11:01:14 -04:00
// BasicProgressBar is packer's basic progress bar.
// Current implementation will always try to keep
// itself at the bottom of a terminal.
type BasicProgressBar struct {
*pb.ProgressBar
2018-09-12 19:21:58 -04:00
finishCb func()
}
func NewProgressBar(identifier string) *BasicProgressBar {
bar := new(BasicProgressBar)
bar.ProgressBar = pb.New(0)
return bar
}
var _ ProgressBar = new(BasicProgressBar)
func (bpb *BasicProgressBar) Start(total int64) {
bpb.SetTotal64(total)
bpb.ProgressBar.Start()
}
2018-09-12 19:21:58 -04:00
func (bpb *BasicProgressBar) Finish() {
if bpb.finishCb != nil {
bpb.finishCb()
}
bpb.ProgressBar.Finish()
}
func (bpb *BasicProgressBar) Add(current int64) {
bpb.ProgressBar.Add64(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-06 11:01:14 -04:00
// NoopProgressBar is a silent progress bar.
type NoopProgressBar struct {
}
var _ ProgressBar = new(NoopProgressBar)
func (npb *NoopProgressBar) Start(int64) {}
func (npb *NoopProgressBar) Add(int64) {}
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 }
// ProxyReader implements io.ReadCloser but sends
2018-09-06 11:01:14 -04:00
// count of read bytes to a 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(int64(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
}