From 8ce8171ec15a6dbcb53563d1daefb2a66a73b302 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 2 Jul 2013 12:28:25 -0700 Subject: [PATCH] packer: PrefixedUi properly prefixes each line of output --- packer/ui.go | 20 ++++++++++++++++---- packer/ui_test.go | 3 +++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packer/ui.go b/packer/ui.go index b31f051a1..271f3a219 100644 --- a/packer/ui.go +++ b/packer/ui.go @@ -1,12 +1,14 @@ package packer import ( + "bytes" "errors" "fmt" "io" "log" "os" "os/signal" + "strings" "sync" ) @@ -86,19 +88,29 @@ func (u *ColoredUi) colorize(message string, color UiColor, bold bool) string { } func (u *PrefixedUi) Ask(query string) (string, error) { - return u.Ui.Ask(fmt.Sprintf("%s: %s", u.SayPrefix, query)) + return u.Ui.Ask(u.prefixLines(u.SayPrefix, query)) } func (u *PrefixedUi) Say(message string) { - u.Ui.Say(fmt.Sprintf("%s: %s", u.SayPrefix, message)) + u.Ui.Say(u.prefixLines(u.SayPrefix, message)) } func (u *PrefixedUi) Message(message string) { - u.Ui.Message(fmt.Sprintf("%s: %s", u.MessagePrefix, message)) + u.Ui.Message(u.prefixLines(u.MessagePrefix, message)) } func (u *PrefixedUi) Error(message string) { - u.Ui.Error(fmt.Sprintf("%s: %s", u.SayPrefix, message)) + u.Ui.Error(u.prefixLines(u.SayPrefix, message)) +} + +func (u *PrefixedUi) prefixLines(prefix, message string) string { + var result bytes.Buffer + + for _, line := range strings.Split(message, "\n") { + result.WriteString(fmt.Sprintf("%s: %s\n", prefix, line)) + } + + return strings.TrimSpace(result.String()) } func (rw *ReaderWriterUi) Ask(query string) (string, error) { diff --git a/packer/ui_test.go b/packer/ui_test.go index bc3191a86..6a48a80a3 100644 --- a/packer/ui_test.go +++ b/packer/ui_test.go @@ -50,6 +50,9 @@ func TestPrefixedUi(t *testing.T) { prefixUi.Error("bar") assert.Equal(readWriter(bufferUi), "mitchell: bar\n", "should have prefix") + + prefixUi.Say("foo\nbar") + assert.Equal(readWriter(bufferUi), "mitchell: foo\nmitchell: bar\n", "should multiline") } func TestColoredUi_ImplUi(t *testing.T) {