2020-07-04 21:00:11 +00:00
|
|
|
package progress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/logrusorgru/aurora"
|
|
|
|
"github.com/vbauerster/mpb/v5"
|
2020-07-29 21:11:17 +00:00
|
|
|
"github.com/vbauerster/mpb/v5/cwriter"
|
2020-07-04 21:00:11 +00:00
|
|
|
"github.com/vbauerster/mpb/v5/decor"
|
2020-07-29 21:11:17 +00:00
|
|
|
"io"
|
2020-07-04 21:00:11 +00:00
|
|
|
"os"
|
2020-07-29 21:11:17 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2020-07-04 21:00:11 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2020-07-11 20:57:44 +00:00
|
|
|
// Encapsulates progress tracking.
|
2020-07-04 21:00:11 +00:00
|
|
|
type Progress struct {
|
2020-07-24 22:46:18 +00:00
|
|
|
progress *mpb.Progress
|
2020-07-25 21:13:58 +00:00
|
|
|
gbar *mpb.Bar
|
2020-07-26 22:00:06 +00:00
|
|
|
total int64
|
|
|
|
initialTotal int64
|
|
|
|
totalMutex *sync.Mutex
|
2020-07-09 19:20:00 +00:00
|
|
|
captureData *captureData
|
|
|
|
stdCaptureMutex *sync.Mutex
|
|
|
|
stdout *strings.Builder
|
|
|
|
stderr *strings.Builder
|
2020-07-24 22:46:18 +00:00
|
|
|
colorizer aurora.Aurora
|
2020-07-29 21:11:17 +00:00
|
|
|
termWidth int
|
2020-07-04 21:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 20:57:44 +00:00
|
|
|
// Creates and returns a new progress tracking object.
|
2020-07-12 16:09:29 +00:00
|
|
|
func NewProgress(noColor bool) *Progress {
|
2020-07-29 21:11:17 +00:00
|
|
|
w := cwriter.New(os.Stdout)
|
|
|
|
tw, err := w.GetWidth()
|
|
|
|
if err != nil {
|
|
|
|
panic("Couldn't determine available terminal width.")
|
|
|
|
}
|
|
|
|
|
2020-07-04 21:00:11 +00:00
|
|
|
p := &Progress{
|
|
|
|
progress: mpb.New(
|
|
|
|
mpb.WithOutput(os.Stderr),
|
|
|
|
mpb.PopCompletedMode(),
|
|
|
|
),
|
2020-07-26 22:00:06 +00:00
|
|
|
totalMutex: &sync.Mutex{},
|
2020-07-09 19:20:00 +00:00
|
|
|
stdCaptureMutex: &sync.Mutex{},
|
|
|
|
stdout: &strings.Builder{},
|
|
|
|
stderr: &strings.Builder{},
|
2020-07-24 22:46:18 +00:00
|
|
|
colorizer: aurora.NewAurora(!noColor),
|
2020-07-29 21:11:17 +00:00
|
|
|
termWidth: tw,
|
2020-07-04 21:00:11 +00:00
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-07-11 20:57:44 +00:00
|
|
|
// Creates and returns a progress bar that tracks all the requests progress.
|
|
|
|
// This is only useful when multiple templates are processed within the same run.
|
2020-07-26 13:10:03 +00:00
|
|
|
func (p *Progress) InitProgressbar(hostCount int64, templateCount int, requestCount int64) {
|
2020-07-24 20:30:15 +00:00
|
|
|
if p.gbar != nil {
|
|
|
|
panic("A global progressbar is already present.")
|
|
|
|
}
|
|
|
|
|
2020-07-12 16:09:29 +00:00
|
|
|
color := p.colorizer
|
|
|
|
|
2020-07-24 22:46:18 +00:00
|
|
|
barName := color.Sprintf(
|
2020-07-25 17:58:17 +00:00
|
|
|
color.Cyan("%d %s, %d %s"),
|
2020-07-12 16:09:29 +00:00
|
|
|
color.Bold(color.Cyan(templateCount)),
|
2020-07-25 17:58:17 +00:00
|
|
|
pluralize(int64(templateCount), "template", "templates"),
|
2020-07-12 16:09:29 +00:00
|
|
|
color.Bold(color.Cyan(hostCount)),
|
2020-07-25 17:58:17 +00:00
|
|
|
pluralize(hostCount, "host", "hosts"))
|
2020-07-11 20:57:44 +00:00
|
|
|
|
2020-07-26 13:10:03 +00:00
|
|
|
p.gbar = p.setupProgressbar("["+barName+"]", requestCount, 0)
|
2020-07-11 20:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 19:03:18 +00:00
|
|
|
func pluralize(count int64, singular, plural string) string {
|
|
|
|
if count > 1 {
|
|
|
|
return plural
|
|
|
|
}
|
|
|
|
return singular
|
|
|
|
}
|
|
|
|
|
2020-07-26 22:00:06 +00:00
|
|
|
// Update total progress request count
|
|
|
|
func (p *Progress) AddToTotal(delta int64) {
|
|
|
|
p.totalMutex.Lock()
|
|
|
|
p.total += delta
|
|
|
|
p.gbar.SetTotal(p.total, false)
|
|
|
|
p.totalMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-07-11 20:57:44 +00:00
|
|
|
// Update progress tracking information and increments the request counter by one unit.
|
2020-07-26 13:10:03 +00:00
|
|
|
func (p *Progress) Update() {
|
|
|
|
p.gbar.Increment()
|
2020-07-04 21:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 20:57:44 +00:00
|
|
|
// Drops the specified number of requests from the progress bar total.
|
|
|
|
// This may be the case when uncompleted requests are encountered and shouldn't be part of the total count.
|
2020-07-26 13:10:03 +00:00
|
|
|
func (p *Progress) Drop(count int64) {
|
|
|
|
// mimic dropping by incrementing the completed requests
|
|
|
|
p.gbar.IncrInt64(count)
|
2020-07-26 22:00:06 +00:00
|
|
|
|
2020-07-05 22:09:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 20:57:44 +00:00
|
|
|
// Ensures that a progress bar's total count is up-to-date if during an enumeration there were uncompleted requests and
|
|
|
|
// wait for all the progress bars to finish.
|
2020-07-04 21:00:11 +00:00
|
|
|
func (p *Progress) Wait() {
|
2020-07-26 22:00:06 +00:00
|
|
|
p.totalMutex.Lock()
|
2020-07-27 18:39:13 +00:00
|
|
|
if p.total == 0 {
|
|
|
|
p.gbar.Abort(true)
|
|
|
|
} else if p.initialTotal != p.total {
|
2020-07-26 22:00:06 +00:00
|
|
|
p.gbar.SetTotal(p.total, true)
|
|
|
|
}
|
|
|
|
p.totalMutex.Unlock()
|
2020-07-04 21:00:11 +00:00
|
|
|
p.progress.Wait()
|
|
|
|
}
|
|
|
|
|
2020-07-11 20:57:44 +00:00
|
|
|
// Creates and returns a progress bar.
|
2020-07-24 20:30:15 +00:00
|
|
|
func (p *Progress) setupProgressbar(name string, total int64, priority int) *mpb.Bar {
|
2020-07-12 16:09:29 +00:00
|
|
|
color := p.colorizer
|
2020-07-24 22:46:18 +00:00
|
|
|
|
2020-07-26 22:00:06 +00:00
|
|
|
p.total = total
|
|
|
|
p.initialTotal = total
|
|
|
|
|
2020-07-11 20:57:44 +00:00
|
|
|
return p.progress.AddBar(
|
|
|
|
total,
|
2020-07-24 20:30:15 +00:00
|
|
|
mpb.BarPriority(priority),
|
2020-07-11 20:57:44 +00:00
|
|
|
mpb.BarNoPop(),
|
|
|
|
mpb.BarRemoveOnComplete(),
|
|
|
|
mpb.PrependDecorators(
|
2020-07-25 19:03:18 +00:00
|
|
|
decor.Name(name, decor.WCSyncSpaceR),
|
2020-07-25 18:29:12 +00:00
|
|
|
decor.CountersNoUnit(color.BrightBlue(" %d/%d").String(), decor.WCSyncSpace),
|
2020-07-26 13:10:03 +00:00
|
|
|
decor.NewPercentage(color.Bold("%d").String(), decor.WCSyncSpace),
|
2020-07-11 20:57:44 +00:00
|
|
|
),
|
|
|
|
mpb.AppendDecorators(
|
2020-07-26 22:00:06 +00:00
|
|
|
decor.AverageSpeed(0, color.BrightYellow("%.2f").Bold().String()+color.BrightYellow("r/s").String(), decor.WCSyncSpace),
|
2020-07-12 16:09:29 +00:00
|
|
|
decor.Elapsed(decor.ET_STYLE_GO, decor.WCSyncSpace),
|
2020-07-11 20:57:44 +00:00
|
|
|
decor.AverageETA(decor.ET_STYLE_GO, decor.WCSyncSpace),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2020-07-04 21:00:11 +00:00
|
|
|
|
2020-07-11 20:57:44 +00:00
|
|
|
// Starts capturing stdout and stderr instead of producing visual output that may interfere with the progress bars.
|
2020-07-04 21:00:11 +00:00
|
|
|
func (p *Progress) StartStdCapture() {
|
2020-07-09 19:20:00 +00:00
|
|
|
p.stdCaptureMutex.Lock()
|
2020-07-04 21:00:11 +00:00
|
|
|
p.captureData = startStdCapture()
|
|
|
|
}
|
|
|
|
|
2020-07-11 20:57:44 +00:00
|
|
|
// Stops capturing stdout and stderr and store both output to be shown later.
|
2020-07-09 18:57:24 +00:00
|
|
|
func (p *Progress) StopStdCapture() {
|
2020-07-04 21:00:11 +00:00
|
|
|
stopStdCapture(p.captureData)
|
2020-07-09 18:57:24 +00:00
|
|
|
p.stdout.Write(p.captureData.DataStdOut.Bytes())
|
|
|
|
p.stderr.Write(p.captureData.DataStdErr.Bytes())
|
2020-07-29 21:11:17 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
var r = regexp.MustCompile("(.{" + strconv.Itoa(p.termWidth) + "})")
|
|
|
|
multiline := r.ReplaceAllString(p.stdout.String(), "$1\n")
|
|
|
|
arr := strings.Split(multiline, "\n")
|
|
|
|
for _, msg := range arr {
|
|
|
|
if len(msg) > 0 {
|
|
|
|
p.progress.Add(0, makeLogBar(msg)).SetTotal(0, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.stdout.Reset()
|
|
|
|
//
|
|
|
|
|
2020-07-09 19:20:00 +00:00
|
|
|
p.stdCaptureMutex.Unlock()
|
2020-07-09 18:57:24 +00:00
|
|
|
}
|
2020-07-08 21:13:53 +00:00
|
|
|
|
2020-07-11 20:57:44 +00:00
|
|
|
// Writes the captured stdout data to stdout, if any.
|
2020-07-09 18:57:24 +00:00
|
|
|
func (p *Progress) ShowStdOut() {
|
2020-07-29 21:11:17 +00:00
|
|
|
//if p.stdout.Len() > 0 {
|
|
|
|
// fmt.Fprint(os.Stdout, p.stdout.String())
|
|
|
|
//}
|
2020-07-04 21:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 20:57:44 +00:00
|
|
|
// Writes the captured stderr data to stderr, if any.
|
2020-07-09 18:57:24 +00:00
|
|
|
func (p *Progress) ShowStdErr() {
|
2020-07-29 21:11:17 +00:00
|
|
|
//if p.stderr.Len() > 0 {
|
|
|
|
// fmt.Fprint(os.Stderr, p.stderr.String())
|
|
|
|
//}
|
2020-07-04 21:00:11 +00:00
|
|
|
}
|
2020-07-29 21:11:17 +00:00
|
|
|
|
|
|
|
func makeLogBar(msg string) mpb.BarFiller {
|
|
|
|
return mpb.BarFillerFunc(func(w io.Writer, _ int, st decor.Statistics) {
|
|
|
|
fmt.Fprintf(w, msg)
|
|
|
|
})
|
|
|
|
}
|