Commit 7198936f authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: PrefixedUi properly prefixes each line of output

parent 874b4acf
package packer package packer
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strings"
"sync" "sync"
) )
...@@ -86,19 +88,29 @@ func (u *ColoredUi) colorize(message string, color UiColor, bold bool) string { ...@@ -86,19 +88,29 @@ func (u *ColoredUi) colorize(message string, color UiColor, bold bool) string {
} }
func (u *PrefixedUi) Ask(query string) (string, error) { 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) { 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) { 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) { 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) { func (rw *ReaderWriterUi) Ask(query string) (string, error) {
......
...@@ -50,6 +50,9 @@ func TestPrefixedUi(t *testing.T) { ...@@ -50,6 +50,9 @@ func TestPrefixedUi(t *testing.T) {
prefixUi.Error("bar") prefixUi.Error("bar")
assert.Equal(readWriter(bufferUi), "mitchell: bar\n", "should have prefix") 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) { func TestColoredUi_ImplUi(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment