2018-09-04 11:57:21 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
2018-09-20 16:14:17 -04:00
|
|
|
"fmt"
|
2018-09-05 06:50:53 -04:00
|
|
|
"io"
|
2018-09-05 11:15:54 -04:00
|
|
|
"sync"
|
2018-09-20 16:14:17 -04:00
|
|
|
"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 {
|
2018-09-06 10:49:15 -04:00
|
|
|
Start(total int64)
|
|
|
|
Add(current int64)
|
2018-09-05 06:50:53 -04:00
|
|
|
NewProxyReader(r io.Reader) (proxy io.Reader)
|
2018-09-04 11:57:21 -04:00
|
|
|
Finish()
|
|
|
|
}
|
|
|
|
|
2018-09-20 16:14:17 -04:00
|
|
|
// StackableProgressBar is a progress bar that
|
|
|
|
// allows to track multiple downloads at once.
|
|
|
|
// Every call to Start increments a counter that
|
|
|
|
// will display the number of current loadings.
|
|
|
|
// Every call to Start will add total to an internal
|
|
|
|
// total that is the total displayed.
|
|
|
|
// First call to Start will start a goroutine
|
|
|
|
// that is waiting for every download to be finished.
|
|
|
|
// Last call to Finish triggers a cleanup.
|
|
|
|
// When all active downloads are finished
|
|
|
|
// StackableProgressBar will clean itself to a default
|
|
|
|
// state.
|
2018-09-05 11:15:54 -04:00
|
|
|
type StackableProgressBar struct {
|
2018-09-06 12:12:15 -04:00
|
|
|
mtx sync.Mutex // locks in Start & Finish
|
2018-09-20 16:14:17 -04:00
|
|
|
BasicProgressBar
|
|
|
|
items int32
|
|
|
|
total int64
|
2018-09-06 12:12:15 -04:00
|
|
|
|
2018-09-20 16:14:17 -04:00
|
|
|
started bool
|
2018-09-05 11:15:54 -04:00
|
|
|
}
|
|
|
|
|
2018-09-20 16:14:17 -04:00
|
|
|
var _ ProgressBar = new(StackableProgressBar)
|
2018-09-06 12:12:15 -04:00
|
|
|
|
2018-09-20 16:14:17 -04:00
|
|
|
func (spb *StackableProgressBar) start() {
|
|
|
|
spb.BasicProgressBar.ProgressBar = pb.New(0)
|
|
|
|
spb.BasicProgressBar.ProgressBar.SetUnits(pb.U_BYTES)
|
2018-09-05 12:22:45 -04:00
|
|
|
|
2018-09-20 16:14:17 -04:00
|
|
|
spb.BasicProgressBar.ProgressBar.Start()
|
|
|
|
spb.started = true
|
2018-09-05 11:15:54 -04:00
|
|
|
}
|
|
|
|
|
2018-09-20 16:14:17 -04:00
|
|
|
func (spb *StackableProgressBar) Start(total int64) {
|
2018-09-06 12:12:15 -04:00
|
|
|
spb.mtx.Lock()
|
|
|
|
|
2018-09-20 16:14:17 -04:00
|
|
|
spb.total += total
|
|
|
|
spb.items++
|
|
|
|
|
|
|
|
if !spb.started {
|
|
|
|
spb.start()
|
2018-09-06 12:12:15 -04:00
|
|
|
}
|
2018-09-20 16:14:17 -04:00
|
|
|
spb.SetTotal64(spb.total)
|
|
|
|
spb.prefix()
|
|
|
|
spb.mtx.Unlock()
|
|
|
|
}
|
2018-09-12 19:21:58 -04:00
|
|
|
|
2018-09-20 16:14:17 -04:00
|
|
|
func (spb *StackableProgressBar) prefix() {
|
|
|
|
spb.BasicProgressBar.ProgressBar.Prefix(fmt.Sprintf("%d items: ", atomic.LoadInt32(&spb.items)))
|
|
|
|
}
|
2018-09-12 19:21:58 -04:00
|
|
|
|
2018-09-20 16:14:17 -04:00
|
|
|
func (spb *StackableProgressBar) Finish() {
|
|
|
|
spb.mtx.Lock()
|
|
|
|
defer spb.mtx.Unlock()
|
|
|
|
|
|
|
|
spb.items--
|
|
|
|
if spb.items == 0 {
|
|
|
|
// slef cleanup
|
|
|
|
spb.BasicProgressBar.ProgressBar.Finish()
|
|
|
|
spb.BasicProgressBar.ProgressBar = nil
|
|
|
|
spb.started = false
|
|
|
|
spb.total = 0
|
|
|
|
return
|
2018-09-12 20:04:10 -04:00
|
|
|
}
|
2018-09-20 16:14:17 -04:00
|
|
|
spb.prefix()
|
2018-09-05 11:15:54 -04:00
|
|
|
}
|
|
|
|
|
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.
|
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-06 10:49:15 -04:00
|
|
|
func (bpb *BasicProgressBar) Start(total int64) {
|
2018-09-06 12:12:15 -04:00
|
|
|
bpb.SetTotal64(total)
|
2018-09-04 11:57:21 -04:00
|
|
|
bpb.ProgressBar.Start()
|
|
|
|
}
|
|
|
|
|
2018-09-06 10:49:15 -04:00
|
|
|
func (bpb *BasicProgressBar) Add(current int64) {
|
2018-09-06 12:12:15 -04:00
|
|
|
bpb.ProgressBar.Add64(current)
|
2018-09-05 06:50:53 -04:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-06 11:01:14 -04:00
|
|
|
// NoopProgressBar is a silent progress bar.
|
2018-09-04 11:57:21 -04:00
|
|
|
type NoopProgressBar struct {
|
|
|
|
}
|
|
|
|
|
2018-09-05 11:15:54 -04:00
|
|
|
var _ ProgressBar = new(NoopProgressBar)
|
|
|
|
|
2018-09-06 10:49:15 -04:00
|
|
|
func (npb *NoopProgressBar) Start(int64) {}
|
|
|
|
func (npb *NoopProgressBar) Add(int64) {}
|
2018-09-05 06:50:53 -04:00
|
|
|
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
|
2018-09-06 11:01:14 -04:00
|
|
|
// count of read bytes to a progress bar
|
2018-09-05 06:50:53 -04:00
|
|
|
type ProxyReader struct {
|
|
|
|
io.Reader
|
|
|
|
ProgressBar
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ProxyReader) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = r.Reader.Read(p)
|
2018-09-06 10:49:15 -04:00
|
|
|
r.ProgressBar.Add(int64(n))
|
2018-09-05 06:50:53 -04:00
|
|
|
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
|
|
|
|
}
|