Commit 2c4a873a authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Add PrefixedUi

parent cc4970d4
...@@ -14,6 +14,13 @@ type Ui interface { ...@@ -14,6 +14,13 @@ type Ui interface {
Error(format string, a ...interface{}) Error(format string, a ...interface{})
} }
// PrefixedUi is a UI that wraps another UI implementation and adds a
// prefix to all the messages going out.
type PrefixedUi struct {
Prefix string
Ui Ui
}
// The ReaderWriterUi is a UI that writes and reads from standard Go // The ReaderWriterUi is a UI that writes and reads from standard Go
// io.Reader and io.Writer. // io.Reader and io.Writer.
type ReaderWriterUi struct { type ReaderWriterUi struct {
...@@ -21,6 +28,14 @@ type ReaderWriterUi struct { ...@@ -21,6 +28,14 @@ type ReaderWriterUi struct {
Writer io.Writer Writer io.Writer
} }
func (u *PrefixedUi) Say(format string, a ...interface{}) {
u.Ui.Say(fmt.Sprintf("%s: %s", u.Prefix, format), a...)
}
func (u *PrefixedUi) Error(format string, a ...interface{}) {
u.Ui.Error(fmt.Sprintf("%s: %s", u.Prefix, format), a...)
}
func (rw *ReaderWriterUi) Say(format string, a ...interface{}) { func (rw *ReaderWriterUi) Say(format string, a ...interface{}) {
output := fmt.Sprintf(format, a...) output := fmt.Sprintf(format, a...)
log.Printf("ui: %s", output) log.Printf("ui: %s", output)
......
...@@ -13,6 +13,35 @@ func testUi() *ReaderWriterUi { ...@@ -13,6 +13,35 @@ func testUi() *ReaderWriterUi {
} }
} }
func TestPrefixedUi(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
bufferUi := testUi()
prefixUi := &PrefixedUi{"mitchell", bufferUi}
prefixUi.Say("foo")
assert.Equal(readWriter(bufferUi), "mitchell: foo\n", "should have prefix")
prefixUi.Error("bar")
assert.Equal(readWriter(bufferUi), "mitchell: bar\n", "should have prefix")
}
func TestPrefixedUi_ImplUi(t *testing.T) {
var raw interface{}
raw = &PrefixedUi{}
if _, ok := raw.(Ui); !ok {
t.Fatalf("PrefixedUi must implement Ui")
}
}
func TestReaderWriterUi_ImplUi(t *testing.T) {
var raw interface{}
raw = &ReaderWriterUi{}
if _, ok := raw.(Ui); !ok {
t.Fatalf("ReaderWriterUi must implement Ui")
}
}
func TestReaderWriterUi_Error(t *testing.T) { func TestReaderWriterUi_Error(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
......
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