diff --git a/main.go b/main.go index 6fb8c7c7b..47f69d364 100644 --- a/main.go +++ b/main.go @@ -196,7 +196,7 @@ func wrappedMain() int { Reader: os.Stdin, Writer: os.Stdout, ErrorWriter: os.Stdout, - PB: &packer.UiProgressBar{}, + PB: &packer.NoopProgressTracker{}, } ui = basicUi if !inPlugin { @@ -208,12 +208,11 @@ func wrappedMain() int { } if backgrounded { fmt.Fprint(os.Stderr, "Running in background, not using a TTY\n") - basicUi.PB = &packer.NoopProgressTracker{} } else if TTY, err := openTTY(); err != nil { fmt.Fprintf(os.Stderr, "No tty available: %s\n", err) - basicUi.PB = &packer.NoopProgressTracker{} } else { basicUi.TTY = TTY + basicUi.PB = &packer.UiProgressBar{} defer TTY.Close() } } diff --git a/packer/progressbar.go b/packer/progressbar.go index fedc163a7..b6feefffd 100644 --- a/packer/progressbar.go +++ b/packer/progressbar.go @@ -15,14 +15,8 @@ func ProgressBarConfig(bar *pb.ProgressBar, prefix string) { bar.Prefix(prefix) } -var defaultUiProgressBar = &UiProgressBar{} - -// UiProgressBar is a self managed progress bar singleton. -// decorate your struct with a *UiProgressBar to -// give it TrackProgress capabilities. -// In TrackProgress if UiProgressBar is nil -// defaultUiProgressBar will be used as -// the progress bar. +// UiProgressBar is a progress bar compatible with go-getter used in our +// UI structs. type UiProgressBar struct { Noop bool lock sync.Mutex @@ -30,10 +24,9 @@ type UiProgressBar struct { pbs int } +var noOpProgressBar *UiProgressBar = nil + func (p *UiProgressBar) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser { - if p == nil { - return defaultUiProgressBar.TrackProgress(src, currentSize, totalSize, stream) - } if p.Noop { return stream } diff --git a/packer/ui_test.go b/packer/ui_test.go index bae2ca570..9684aaf34 100644 --- a/packer/ui_test.go +++ b/packer/ui_test.go @@ -49,7 +49,7 @@ func (tty *testTTY) ReadString() (string, error) { func TestColoredUi(t *testing.T) { bufferUi := testUi() - ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi, defaultUiProgressBar} + ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi, &UiProgressBar{}} if !ui.supportsColors() { t.Skip("skipping for ui without color support") @@ -81,7 +81,7 @@ func TestColoredUi(t *testing.T) { func TestColoredUi_noColorEnv(t *testing.T) { bufferUi := testUi() - ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi, defaultUiProgressBar} + ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi, &UiProgressBar{}} // Set the env var to get rid of the color oldenv := os.Getenv("PACKER_NO_COLOR")