From 10b0f920bc5440682aa6f0a7e498a7342185fd5b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 11 Aug 2013 18:31:28 -0700 Subject: [PATCH] packer: renamed PrefixedUi to TargettedUi --- packer/build.go | 16 +++++++--------- packer/ui.go | 41 ++++++++++++++++++++++++----------------- packer/ui_test.go | 29 ++++++++++++++++------------- 3 files changed, 47 insertions(+), 39 deletions(-) diff --git a/packer/build.go b/packer/build.go index bff34767e..39e54a559 100644 --- a/packer/build.go +++ b/packer/build.go @@ -213,11 +213,10 @@ func (b *coreBuild) Run(originalUi Ui, cache Cache) ([]Artifact, error) { hook := &DispatchHook{hooks} artifacts := make([]Artifact, 0, 1) - // The builder just has a normal Ui, but prefixed - builderUi := &PrefixedUi{ - fmt.Sprintf("==> %s", b.Name()), - fmt.Sprintf(" %s", b.Name()), - originalUi, + // The builder just has a normal Ui, but targetted + builderUi := &TargettedUi{ + Target: b.Name(), + Ui: originalUi, } log.Printf("Running builder: %s", b.builderType) @@ -240,10 +239,9 @@ PostProcessorRunSeqLoop: for _, ppSeq := range b.postProcessors { priorArtifact := builderArtifact for i, corePP := range ppSeq { - ppUi := &PrefixedUi{ - fmt.Sprintf("==> %s (%s)", b.Name(), corePP.processorType), - fmt.Sprintf(" %s (%s)", b.Name(), corePP.processorType), - originalUi, + ppUi := &TargettedUi{ + Target: fmt.Sprintf("%s (%s)", b.Name(), corePP.processorType), + Ui: originalUi, } builderUi.Say(fmt.Sprintf("Running post-processor: %s", corePP.processorType)) diff --git a/packer/ui.go b/packer/ui.go index d6c97f355..62d44a630 100644 --- a/packer/ui.go +++ b/packer/ui.go @@ -43,12 +43,14 @@ type ColoredUi struct { Ui Ui } -// PrefixedUi is a UI that wraps another UI implementation and adds a -// prefix to all the messages going out. -type PrefixedUi struct { - SayPrefix string - MessagePrefix string - Ui Ui +// TargettedUi is a UI that wraps another UI implementation and modifies +// the output to indicate a specific target. Specifically, all Say output +// is prefixed with the target name. Message output is not prefixed but +// is offset by the length of the target so that output is lined up properly +// with Say output. Machine-readable output has the proper target set. +type TargettedUi struct { + Target string + Ui Ui } // The BasicUI is a UI that reads and writes from a standard Go reader @@ -116,32 +118,37 @@ func (u *ColoredUi) supportsColors() bool { return cygwin } -func (u *PrefixedUi) Ask(query string) (string, error) { - return u.Ui.Ask(u.prefixLines(u.SayPrefix, query)) +func (u *TargettedUi) Ask(query string) (string, error) { + return u.Ui.Ask(u.prefixLines(true, query)) } -func (u *PrefixedUi) Say(message string) { - u.Ui.Say(u.prefixLines(u.SayPrefix, message)) +func (u *TargettedUi) Say(message string) { + u.Ui.Say(u.prefixLines(true, message)) } -func (u *PrefixedUi) Message(message string) { - u.Ui.Message(u.prefixLines(u.MessagePrefix, message)) +func (u *TargettedUi) Message(message string) { + u.Ui.Message(u.prefixLines(false, message)) } -func (u *PrefixedUi) Error(message string) { - u.Ui.Error(u.prefixLines(u.SayPrefix, message)) +func (u *TargettedUi) Error(message string) { + u.Ui.Error(u.prefixLines(true, message)) } -func (u *PrefixedUi) Machine(t string, args ...string) { +func (u *TargettedUi) Machine(t string, args ...string) { // Just pass it through for now. u.Ui.Machine(t, args...) } -func (u *PrefixedUi) prefixLines(prefix, message string) string { +func (u *TargettedUi) prefixLines(arrow bool, message string) string { + arrowText := "==>" + if !arrow { + arrowText = strings.Repeat(" ", len(arrowText)) + } + var result bytes.Buffer for _, line := range strings.Split(message, "\n") { - result.WriteString(fmt.Sprintf("%s: %s\n", prefix, line)) + result.WriteString(fmt.Sprintf("%s %s: %s\n", arrowText, u.Target, line)) } return strings.TrimRightFunc(result.String(), unicode.IsSpace) diff --git a/packer/ui_test.go b/packer/ui_test.go index b42255f15..13ed53a18 100644 --- a/packer/ui_test.go +++ b/packer/ui_test.go @@ -36,23 +36,26 @@ func TestColoredUi(t *testing.T) { } } -func TestPrefixedUi(t *testing.T) { +func TestTargettedUi(t *testing.T) { assert := asserts.NewTestingAsserts(t, true) bufferUi := testUi() - prefixUi := &PrefixedUi{"mitchell", "bar", bufferUi} + TargettedUi := &TargettedUi{ + Target: "foo", + Ui: bufferUi, + } - prefixUi.Say("foo") - assert.Equal(readWriter(bufferUi), "mitchell: foo\n", "should have prefix") + TargettedUi.Say("foo") + assert.Equal(readWriter(bufferUi), "==> foo: foo\n", "should have prefix") - prefixUi.Message("foo") - assert.Equal(readWriter(bufferUi), "bar: foo\n", "should have prefix") + TargettedUi.Message("foo") + assert.Equal(readWriter(bufferUi), " foo: foo\n", "should have prefix") - prefixUi.Error("bar") - assert.Equal(readWriter(bufferUi), "mitchell: bar\n", "should have prefix") + TargettedUi.Error("bar") + assert.Equal(readWriter(bufferUi), "==> foo: bar\n", "should have prefix") - prefixUi.Say("foo\nbar") - assert.Equal(readWriter(bufferUi), "mitchell: foo\nmitchell: bar\n", "should multiline") + TargettedUi.Say("foo\nbar") + assert.Equal(readWriter(bufferUi), "==> foo: foo\n==> foo: bar\n", "should multiline") } func TestColoredUi_ImplUi(t *testing.T) { @@ -63,11 +66,11 @@ func TestColoredUi_ImplUi(t *testing.T) { } } -func TestPrefixedUi_ImplUi(t *testing.T) { +func TestTargettedUi_ImplUi(t *testing.T) { var raw interface{} - raw = &PrefixedUi{} + raw = &TargettedUi{} if _, ok := raw.(Ui); !ok { - t.Fatalf("PrefixedUi must implement Ui") + t.Fatalf("TargettedUi must implement Ui") } }