From ae8d359bf16e5018901f37656c12c49b6217ce63 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 21 Sep 2020 16:05:14 -0700 Subject: [PATCH 1/5] Switch to explicitly declaring TrackProgress on UI, using an underlying progress bar specifically stored on the struct. Export UiProgressTracker. These changes allow us to optionaly switch to the NoopProgressTracker when no TTY is set. --- go.mod | 1 + go.sum | 3 +++ main.go | 3 +++ packer/progressbar.go | 20 ++++++++++--------- packer/progressbar_solaris.go | 2 +- packer/progressbar_test.go | 6 +++--- packer/ui.go | 36 +++++++++++++++++++++++++++++------ 7 files changed, 52 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index 6596057fc..70b4756db 100644 --- a/go.mod +++ b/go.mod @@ -96,6 +96,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/go-testing-interface v1.0.3 // indirect github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed + github.com/mitchellh/gox v1.0.1 // indirect github.com/mitchellh/iochan v1.0.0 github.com/mitchellh/mapstructure v1.2.3 github.com/mitchellh/panicwrap v0.0.0-20170106182340-fce601fe5557 diff --git a/go.sum b/go.sum index a4452f1e7..e64fc9fbf 100644 --- a/go.sum +++ b/go.sum @@ -351,6 +351,7 @@ github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1 github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE= github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0= github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= @@ -487,6 +488,8 @@ github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZX github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4= github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= +github.com/mitchellh/gox v1.0.1 h1:x0jD3dcHk9a9xPSDN6YEL4xL6Qz0dvNYm8yZqui5chI= +github.com/mitchellh/gox v1.0.1/go.mod h1:ED6BioOGXMswlXa2zxfh/xdd5QhwYliBFn9V18Ap4z4= github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY= github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= diff --git a/main.go b/main.go index 8d4c5ee38..6fb8c7c7b 100644 --- a/main.go +++ b/main.go @@ -196,6 +196,7 @@ func wrappedMain() int { Reader: os.Stdin, Writer: os.Stdout, ErrorWriter: os.Stdout, + PB: &packer.UiProgressBar{}, } ui = basicUi if !inPlugin { @@ -207,8 +208,10 @@ 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 defer TTY.Close() diff --git a/packer/progressbar.go b/packer/progressbar.go index 3328ca9ce..fedc163a7 100644 --- a/packer/progressbar.go +++ b/packer/progressbar.go @@ -15,26 +15,28 @@ func ProgressBarConfig(bar *pb.ProgressBar, prefix string) { bar.Prefix(prefix) } -var defaultUiProgressBar = &uiProgressBar{} +var defaultUiProgressBar = &UiProgressBar{} -// uiProgressBar is a self managed progress bar singleton. -// decorate your struct with a *uiProgressBar to +// 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 +// In TrackProgress if UiProgressBar is nil // defaultUiProgressBar will be used as // the progress bar. -type uiProgressBar struct { +type UiProgressBar struct { + Noop bool lock sync.Mutex - pool *pb.Pool - - pbs int + pbs int } -func (p *uiProgressBar) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser { +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 + } p.lock.Lock() defer p.lock.Unlock() diff --git a/packer/progressbar_solaris.go b/packer/progressbar_solaris.go index 75b40ad06..868037c38 100644 --- a/packer/progressbar_solaris.go +++ b/packer/progressbar_solaris.go @@ -1,3 +1,3 @@ package packer -type uiProgressBar = NoopProgressTracker +type UiProgressBar = NoopProgressTracker diff --git a/packer/progressbar_test.go b/packer/progressbar_test.go index 5039c48a9..40928d273 100644 --- a/packer/progressbar_test.go +++ b/packer/progressbar_test.go @@ -11,7 +11,7 @@ import ( // The following tests rarelly just happen. So we run them 100 times. func TestProgressTracking_open_close(t *testing.T) { - var bar *uiProgressBar + var bar *UiProgressBar tracker := bar.TrackProgress("1,", 1, 42, ioutil.NopCloser(nil)) tracker.Close() @@ -21,7 +21,7 @@ func TestProgressTracking_open_close(t *testing.T) { } func TestProgressTracking_multi_open_close(t *testing.T) { - var bar *uiProgressBar + var bar *UiProgressBar g := errgroup.Group{} for i := 0; i < 100; i++ { @@ -36,7 +36,7 @@ func TestProgressTracking_multi_open_close(t *testing.T) { } func TestProgressTracking_races(t *testing.T) { - var bar *uiProgressBar + var bar *UiProgressBar g := errgroup.Group{} for i := 0; i < 100; i++ { diff --git a/packer/ui.go b/packer/ui.go index 5d8cb89ed..46bfbf235 100644 --- a/packer/ui.go +++ b/packer/ui.go @@ -40,11 +40,12 @@ type Ui interface { Message(string) Error(string) Machine(string, ...string) + // TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) getter.ProgressTracker } type NoopUi struct { - NoopProgressTracker + PB NoopProgressTracker } var _ Ui = new(NoopUi) @@ -54,13 +55,16 @@ func (*NoopUi) Say(string) { return } func (*NoopUi) Message(string) { return } func (*NoopUi) Error(string) { return } func (*NoopUi) Machine(string, ...string) { return } +func (u *NoopUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser { + return u.PB.TrackProgress(src, currentSize, totalSize, stream) +} // ColoredUi is a UI that is colored using terminal colors. type ColoredUi struct { Color UiColor ErrorColor UiColor Ui Ui - *uiProgressBar + PB getter.ProgressTracker } var _ Ui = new(ColoredUi) @@ -189,7 +193,7 @@ type BasicUi struct { l sync.Mutex interrupted bool TTY TTY - *uiProgressBar + PB getter.ProgressTracker } var _ Ui = new(BasicUi) @@ -304,11 +308,15 @@ func (rw *BasicUi) Machine(t string, args ...string) { log.Printf("machine readable: %s %#v", t, args) } +func (rw *BasicUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { + return rw.PB.TrackProgress(src, currentSize, totalSize, stream) +} + // MachineReadableUi is a UI that only outputs machine-readable output // to the given Writer. type MachineReadableUi struct { Writer io.Writer - NoopProgressTracker + PB NoopProgressTracker } var _ Ui = new(MachineReadableUi) @@ -366,11 +374,15 @@ func (u *MachineReadableUi) Machine(category string, args ...string) { log.Printf("%d,%s,%s,%s\n", now.Unix(), target, category, argsString) } +func (u *MachineReadableUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { + return u.PB.TrackProgress(src, currentSize, totalSize, stream) +} + // TimestampedUi is a UI that wraps another UI implementation and // prefixes each message with an RFC3339 timestamp type TimestampedUi struct { Ui Ui - *uiProgressBar + PB getter.ProgressTracker } var _ Ui = new(TimestampedUi) @@ -395,6 +407,10 @@ func (u *TimestampedUi) Machine(message string, args ...string) { u.Ui.Machine(message, args...) } +func (u *TimestampedUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { + return u.Ui.TrackProgress(src, currentSize, totalSize, stream) +} + func (u *TimestampedUi) timestampLine(string string) string { return fmt.Sprintf("%v: %v", time.Now().Format(time.RFC3339), string) } @@ -404,7 +420,7 @@ func (u *TimestampedUi) timestampLine(string string) string { type SafeUi struct { Sem chan int Ui Ui - *uiProgressBar + PB getter.ProgressTracker } var _ Ui = new(SafeUi) @@ -440,3 +456,11 @@ func (u *SafeUi) Machine(t string, args ...string) { u.Ui.Machine(t, args...) <-u.Sem } + +func (u *SafeUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { + u.Sem <- 1 + ret := u.Ui.TrackProgress(src, currentSize, totalSize, stream) + <-u.Sem + + return ret +} From a805bc538e60feccd5d83d21582578bc4351f3f6 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 21 Sep 2020 16:23:00 -0700 Subject: [PATCH 2/5] fix tests --- common/step_download_test.go | 1 + common/step_output_dir_test.go | 1 + provisioner/file/provisioner_test.go | 5 +++++ 3 files changed, 7 insertions(+) diff --git a/common/step_download_test.go b/common/step_download_test.go index fa0e690d9..26cd026e9 100644 --- a/common/step_download_test.go +++ b/common/step_download_test.go @@ -245,6 +245,7 @@ func TestStepDownload_download(t *testing.T) { ui := &packer.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), + PB: &packer.NoopProgressTracker{}, } dir := createTempDir(t) diff --git a/common/step_output_dir_test.go b/common/step_output_dir_test.go index 197977151..7a5f58d95 100644 --- a/common/step_output_dir_test.go +++ b/common/step_output_dir_test.go @@ -16,6 +16,7 @@ func testState(t *testing.T) multistep.StateBag { state.Put("ui", &packer.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), + PB: &packer.NoopProgressTracker{}, }) return state } diff --git a/provisioner/file/provisioner_test.go b/provisioner/file/provisioner_test.go index 0986e1e05..dd1d0325c 100644 --- a/provisioner/file/provisioner_test.go +++ b/provisioner/file/provisioner_test.go @@ -126,6 +126,7 @@ func TestProvisionerProvision_SendsFile(t *testing.T) { b := bytes.NewBuffer(nil) ui := &packer.BasicUi{ Writer: b, + PB: &packer.NoopProgressTracker{}, } comm := &packer.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) @@ -184,6 +185,7 @@ func TestProvisionerProvision_SendsFileMultipleFiles(t *testing.T) { b := bytes.NewBuffer(nil) ui := &packer.BasicUi{ Writer: b, + PB: &packer.NoopProgressTracker{}, } comm := &packer.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) @@ -253,6 +255,7 @@ func TestProvisionerProvision_SendsFileMultipleDirs(t *testing.T) { b := bytes.NewBuffer(nil) ui := &packer.BasicUi{ Writer: b, + PB: &packer.NoopProgressTracker{}, } comm := &packer.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) @@ -304,6 +307,7 @@ func TestProvisionerProvision_SendsFileMultipleFilesToFolder(t *testing.T) { b := bytes.NewBuffer(nil) ui := &packer.BasicUi{ Writer: b, + PB: &packer.NoopProgressTracker{}, } comm := &packer.MockCommunicator{} err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) @@ -361,6 +365,7 @@ func TestProvisionDownloadMkdirAll(t *testing.T) { b := bytes.NewBuffer(nil) ui := &packer.BasicUi{ Writer: b, + PB: &packer.NoopProgressTracker{}, } comm := &packer.MockCommunicator{} err = p.ProvisionDownload(ui, comm) From 7b1826e1079d86e9748cfb13a3d9fa5de8935232 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 23 Sep 2020 11:17:30 -0700 Subject: [PATCH 3/5] tidy go modules --- go.mod | 5 ----- go.sum | 24 ------------------------ 2 files changed, 29 deletions(-) diff --git a/go.mod b/go.mod index 70b4756db..608c6fac7 100644 --- a/go.mod +++ b/go.mod @@ -39,9 +39,6 @@ require ( github.com/go-ole/go-ole v1.2.4 // indirect github.com/go-resty/resty/v2 v2.3.0 github.com/gobwas/glob v0.2.3 - github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee // indirect - github.com/gobwas/pool v0.2.1 // indirect - github.com/gobwas/ws v1.0.4 // indirect github.com/gofrs/flock v0.7.3 github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 github.com/golang/protobuf v1.4.2 // indirect @@ -52,7 +49,6 @@ require ( github.com/gophercloud/gophercloud v0.12.0 github.com/gophercloud/utils v0.0.0-20200508015959-b0167b94122c github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect - github.com/gorilla/websocket v0.0.0-20170319172727-a91eba7f9777 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026 github.com/hashicorp/consul/api v1.4.0 @@ -96,7 +92,6 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/go-testing-interface v1.0.3 // indirect github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed - github.com/mitchellh/gox v1.0.1 // indirect github.com/mitchellh/iochan v1.0.0 github.com/mitchellh/mapstructure v1.2.3 github.com/mitchellh/panicwrap v0.0.0-20170106182340-fce601fe5557 diff --git a/go.sum b/go.sum index e64fc9fbf..ea9503fe2 100644 --- a/go.sum +++ b/go.sum @@ -157,8 +157,6 @@ github.com/digitalocean/godo v1.11.1 h1:OsTh37YFKk+g6DnAOrkXJ9oDArTkRx5UTkBJ2EWA github.com/digitalocean/godo v1.11.1/go.mod h1:h6faOIcZ8lWIwNQ+DN7b3CgX4Kwby5T+nbpNqkUIozU= github.com/dimchansky/utfbom v1.1.0 h1:FcM3g+nofKgUteL8dm/UpdRXNC9KmADgTpLKsu0TRo4= github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8= -github.com/diskfs/go-diskfs v1.1.1 h1:rMjLpaydtXGVZb7mdkRGK1+//30i76nKAit89zUzeaI= -github.com/diskfs/go-diskfs v1.1.1/go.mod h1:afUPxxu+x1snp4aCY2bKR0CoZ/YFJewV3X2UEr2nPZE= github.com/dnaeon/go-vcr v1.0.1 h1:r8L/HqC0Hje5AXMu1ooW8oyQyOFv4GxqpL0nRP7SLLY= github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E= github.com/dylanmei/iso8601 v0.1.0 h1:812NGQDBcqquTfH5Yeo7lwR0nzx/cKdsmf3qMjPURUI= @@ -202,12 +200,6 @@ github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= -github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0= -github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= -github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= -github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= -github.com/gobwas/ws v1.0.4 h1:5eXU1CZhpQdq5kXbKb+sECH5Ia5KiO6CYzIzdlVx6Bs= -github.com/gobwas/ws v1.0.4/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= github.com/gofrs/flock v0.7.3 h1:I0EKY9l8HZCXTMYC4F80vwT6KNypV9uYKP3Alm/hjmQ= github.com/gofrs/flock v0.7.3/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE= @@ -284,8 +276,6 @@ github.com/gophercloud/utils v0.0.0-20200508015959-b0167b94122c h1:iawx2ojEQA7c+ github.com/gophercloud/utils v0.0.0-20200508015959-b0167b94122c/go.mod h1:ehWUbLQJPqS0Ep+CxeD559hsm9pthPXadJNKwZkp43w= github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg= github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/gorilla/websocket v0.0.0-20170319172727-a91eba7f9777 h1:JIM+OacoOJRU30xpjMf8sulYqjr0ViA3WDrTX6j/yDI= -github.com/gorilla/websocket v0.0.0-20170319172727-a91eba7f9777/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 h1:THDBEeQ9xZ8JEaCLyLQqXMMdRqNr0QAUJTIkQAUtFjg= github.com/grpc-ecosystem/go-grpc-middleware v1.1.0/go.mod h1:f5nM7jw/oeRSadq3xCzHAvxcr8HZnzsqU6ILg/0NiiE= github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026 h1:BpJ2o0OR5FV7vrkDYfXYVJQeMNWa8RhklZOpW2ITAIQ= @@ -351,7 +341,6 @@ github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1 github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE= github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= -github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0= github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= @@ -488,8 +477,6 @@ github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZX github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4= github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= -github.com/mitchellh/gox v1.0.1 h1:x0jD3dcHk9a9xPSDN6YEL4xL6Qz0dvNYm8yZqui5chI= -github.com/mitchellh/gox v1.0.1/go.mod h1:ED6BioOGXMswlXa2zxfh/xdd5QhwYliBFn9V18Ap4z4= github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY= github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= @@ -590,14 +577,6 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/sylviamoss/go-vnc v0.0.0-20200908143403-27ce26f2bed9 h1:uEIT4Mi8C8O+S44150F3lug+WaZ01aJ0sLHvN4npMsc= -github.com/sylviamoss/go-vnc v0.0.0-20200908143403-27ce26f2bed9/go.mod h1:+I9wdVxvb9OqA4vpPvYoLvAeSx+Ew5PF6SiOWekjkGE= -github.com/sylviamoss/go-vnc v0.0.0-20200908143844-452d6786ec66 h1:4pQ2Ex/3Fxn7WDvpa85XqHLkc+cSoceIefX/8mD518o= -github.com/sylviamoss/go-vnc v0.0.0-20200908143844-452d6786ec66/go.mod h1:+I9wdVxvb9OqA4vpPvYoLvAeSx+Ew5PF6SiOWekjkGE= -github.com/sylviamoss/go-vnc v0.0.0-20200908144818-694f171108bf h1:HUHmcnxuPAJktJM1acw5AckmPvOWz/BoenBLB+IMuo0= -github.com/sylviamoss/go-vnc v0.0.0-20200908144818-694f171108bf/go.mod h1:+I9wdVxvb9OqA4vpPvYoLvAeSx+Ew5PF6SiOWekjkGE= -github.com/sylviamoss/go-vnc v0.0.0-20200908145922-2061e37ffa00 h1:mURYI59JxRHmjEKY4ZZING6qm6e/R91tUqdwFmHoAjY= -github.com/sylviamoss/go-vnc v0.0.0-20200908145922-2061e37ffa00/go.mod h1:+I9wdVxvb9OqA4vpPvYoLvAeSx+Ew5PF6SiOWekjkGE= github.com/tencentcloud/tencentcloud-sdk-go v3.0.222+incompatible h1:bs+0lcG4RELNbE8PsBC9oaPP0/qExr0DuEGnZyocm84= github.com/tencentcloud/tencentcloud-sdk-go v3.0.222+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 h1:G3dpKMzFDjgEh2q1Z7zUUtKa8ViPtH+ocF0bE0g00O8= @@ -766,7 +745,6 @@ golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80= golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -887,8 +865,6 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogR gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/cheggaaa/pb.v1 v1.0.27 h1:kJdccidYzt3CaHD1crCFTS1hxyhSi059NhOFUf03YFo= gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw= -gopkg.in/djherbis/times.v1 v1.2.0 h1:UCvDKl1L/fmBygl2Y7hubXCnY7t4Yj46ZrBFNUipFbM= -gopkg.in/djherbis/times.v1 v1.2.0/go.mod h1:AQlg6unIsrsCEdQYhTzERy542dz6SFdQFZFv6mUY0P8= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.42.0 h1:7N3gPTt50s8GuLortA00n8AqRTk75qOP98+mTPpgzRk= gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= From 15ee44e8576c3c0dd990ce419c1006b33d3fb8d5 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 23 Sep 2020 11:33:51 -0700 Subject: [PATCH 4/5] review comments --- main.go | 5 ++--- packer/progressbar.go | 15 ++++----------- packer/ui_test.go | 4 ++-- 3 files changed, 8 insertions(+), 16 deletions(-) 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") From a9107a8fe90e53764974efa6a16edb172b3d9c87 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 24 Sep 2020 10:52:45 +0200 Subject: [PATCH 5/5] noop progress bar is nil progress bar --- packer/progressbar.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packer/progressbar.go b/packer/progressbar.go index b6feefffd..97e781d74 100644 --- a/packer/progressbar.go +++ b/packer/progressbar.go @@ -18,16 +18,13 @@ func ProgressBarConfig(bar *pb.ProgressBar, prefix string) { // UiProgressBar is a progress bar compatible with go-getter used in our // UI structs. type UiProgressBar struct { - Noop bool lock sync.Mutex pool *pb.Pool pbs int } -var noOpProgressBar *UiProgressBar = nil - func (p *UiProgressBar) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser { - if p.Noop { + if p == nil { return stream } p.lock.Lock()