Commit edab952a authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Add Message method to Ui

parent 37937c12
......@@ -63,6 +63,7 @@ func (c Command) Run(env packer.Environment, args []string) int {
for _, b := range builds {
buildUis[b.Name()] = &packer.PrefixedUi{
fmt.Sprintf("==> %s", b.Name()),
fmt.Sprintf("--> %s", b.Name()),
env.Ui(),
}
}
......
......@@ -23,6 +23,12 @@ func (u *Ui) Error(message string) {
}
}
func (u *Ui) Message(message string) {
if err := u.client.Call("Ui.Message", message, new(interface{})); err != nil {
panic(err)
}
}
func (u *Ui) Say(message string) {
if err := u.client.Call("Ui.Say", message, new(interface{})); err != nil {
panic(err)
......@@ -36,6 +42,12 @@ func (u *UiServer) Error(message *string, reply *interface{}) error {
return nil
}
func (u *UiServer) Message(message *string, reply *interface{}) error {
u.ui.Message(*message)
*reply = nil
return nil
}
func (u *UiServer) Say(message *string, reply *interface{}) error {
u.ui.Say(*message)
......
......@@ -9,6 +9,8 @@ import (
type testUi struct {
errorCalled bool
errorMessage string
messageCalled bool
messageMessage string
sayCalled bool
sayMessage string
}
......@@ -18,6 +20,11 @@ func (u *testUi) Error(message string) {
u.errorMessage = message
}
func (u *testUi) Message(message string) {
u.messageCalled = true
u.messageMessage = message
}
func (u *testUi) Say(message string) {
u.sayCalled = true
u.sayMessage = message
......@@ -46,6 +53,9 @@ func TestUiRPC(t *testing.T) {
uiClient.Error("message")
assert.Equal(ui.errorMessage, "message", "message should be correct")
uiClient.Message("message")
assert.Equal(ui.messageMessage, "message", "message should be correct")
uiClient.Say("message")
assert.Equal(ui.sayMessage, "message", "message should be correct")
}
......@@ -11,13 +11,15 @@ import (
// is formatted and various levels of output.
type Ui interface {
Say(string)
Message(string)
Error(string)
}
// PrefixedUi is a UI that wraps another UI implementation and adds a
// prefix to all the messages going out.
type PrefixedUi struct {
Prefix string
SayPrefix string
MessagePrefix string
Ui Ui
}
......@@ -29,11 +31,15 @@ type ReaderWriterUi struct {
}
func (u *PrefixedUi) Say(message string) {
u.Ui.Say(fmt.Sprintf("%s: %s", u.Prefix, message))
u.Ui.Say(fmt.Sprintf("%s: %s", u.SayPrefix, message))
}
func (u *PrefixedUi) Message(message string) {
u.Ui.Say(fmt.Sprintf("%s: %s", u.MessagePrefix, message))
}
func (u *PrefixedUi) Error(message string) {
u.Ui.Error(fmt.Sprintf("%s: %s", u.Prefix, message))
u.Ui.Error(fmt.Sprintf("%s: %s", u.SayPrefix, message))
}
func (rw *ReaderWriterUi) Say(message string) {
......@@ -44,6 +50,14 @@ func (rw *ReaderWriterUi) Say(message string) {
}
}
func (rw *ReaderWriterUi) Message(message string) {
log.Printf("ui: %s", message)
_, err := fmt.Fprintf(rw.Writer, message+"\n")
if err != nil {
panic(err)
}
}
func (rw *ReaderWriterUi) Error(message string) {
log.Printf("ui error: %s", message)
_, err := fmt.Fprint(rw.Writer, message+"\n")
......
......@@ -17,11 +17,14 @@ func TestPrefixedUi(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
bufferUi := testUi()
prefixUi := &PrefixedUi{"mitchell", bufferUi}
prefixUi := &PrefixedUi{"mitchell", "bar", bufferUi}
prefixUi.Say("foo")
assert.Equal(readWriter(bufferUi), "mitchell: foo\n", "should have prefix")
prefixUi.Message("foo")
assert.Equal(readWriter(bufferUi), "bar: foo\n", "should have prefix")
prefixUi.Error("bar")
assert.Equal(readWriter(bufferUi), "mitchell: bar\n", "should have prefix")
}
......
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