github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/progress_bar.go (about)

     1  package v7action
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"time"
     7  
     8  	"gopkg.in/cheggaaa/pb.v1"
     9  )
    10  
    11  //go:generate counterfeiter . SimpleProgressBar
    12  
    13  type SimpleProgressBar interface {
    14  	Initialize(path string) (io.Reader, int64, error)
    15  	Terminate()
    16  }
    17  
    18  type ProgressBar struct {
    19  	bar *pb.ProgressBar
    20  }
    21  
    22  func NewProgressBar() *ProgressBar {
    23  	return &ProgressBar{}
    24  }
    25  
    26  func (p *ProgressBar) Initialize(path string) (io.Reader, int64, error) {
    27  	file, err := os.Open(path)
    28  	if err != nil {
    29  		return nil, 0, err
    30  	}
    31  
    32  	fileInfo, err := file.Stat()
    33  	if err != nil {
    34  		return nil, 0, err
    35  	}
    36  
    37  	p.bar = pb.New(int(fileInfo.Size())).SetUnits(pb.U_BYTES)
    38  	p.bar.ShowTimeLeft = false
    39  	p.bar.Start()
    40  	return p.bar.NewProxyReader(file), fileInfo.Size(), nil
    41  
    42  }
    43  
    44  func (p *ProgressBar) Terminate() {
    45  	// Adding sleep to ensure UI has finished drawing
    46  	time.Sleep(time.Second)
    47  	p.bar.Finish()
    48  }