packer: Add Message method to Ui

This commit is contained in:
Mitchell Hashimoto 2013-06-03 11:30:38 -07:00
parent 37937c1290
commit edab952a68
5 changed files with 49 additions and 9 deletions

View File

@ -63,6 +63,7 @@ func (c Command) Run(env packer.Environment, args []string) int {
for _, b := range builds { for _, b := range builds {
buildUis[b.Name()] = &packer.PrefixedUi{ buildUis[b.Name()] = &packer.PrefixedUi{
fmt.Sprintf("==> %s", b.Name()), fmt.Sprintf("==> %s", b.Name()),
fmt.Sprintf("--> %s", b.Name()),
env.Ui(), env.Ui(),
} }
} }

View File

@ -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) { func (u *Ui) Say(message string) {
if err := u.client.Call("Ui.Say", message, new(interface{})); err != nil { if err := u.client.Call("Ui.Say", message, new(interface{})); err != nil {
panic(err) panic(err)
@ -36,6 +42,12 @@ func (u *UiServer) Error(message *string, reply *interface{}) error {
return nil 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 { func (u *UiServer) Say(message *string, reply *interface{}) error {
u.ui.Say(*message) u.ui.Say(*message)

View File

@ -7,10 +7,12 @@ import (
) )
type testUi struct { type testUi struct {
errorCalled bool errorCalled bool
errorMessage string errorMessage string
sayCalled bool messageCalled bool
sayMessage string messageMessage string
sayCalled bool
sayMessage string
} }
func (u *testUi) Error(message string) { func (u *testUi) Error(message string) {
@ -18,6 +20,11 @@ func (u *testUi) Error(message string) {
u.errorMessage = message u.errorMessage = message
} }
func (u *testUi) Message(message string) {
u.messageCalled = true
u.messageMessage = message
}
func (u *testUi) Say(message string) { func (u *testUi) Say(message string) {
u.sayCalled = true u.sayCalled = true
u.sayMessage = message u.sayMessage = message
@ -46,6 +53,9 @@ func TestUiRPC(t *testing.T) {
uiClient.Error("message") uiClient.Error("message")
assert.Equal(ui.errorMessage, "message", "message should be correct") assert.Equal(ui.errorMessage, "message", "message should be correct")
uiClient.Message("message")
assert.Equal(ui.messageMessage, "message", "message should be correct")
uiClient.Say("message") uiClient.Say("message")
assert.Equal(ui.sayMessage, "message", "message should be correct") assert.Equal(ui.sayMessage, "message", "message should be correct")
} }

View File

@ -11,14 +11,16 @@ import (
// is formatted and various levels of output. // is formatted and various levels of output.
type Ui interface { type Ui interface {
Say(string) Say(string)
Message(string)
Error(string) Error(string)
} }
// PrefixedUi is a UI that wraps another UI implementation and adds a // PrefixedUi is a UI that wraps another UI implementation and adds a
// prefix to all the messages going out. // prefix to all the messages going out.
type PrefixedUi struct { type PrefixedUi struct {
Prefix string SayPrefix string
Ui Ui MessagePrefix 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
@ -29,11 +31,15 @@ type ReaderWriterUi struct {
} }
func (u *PrefixedUi) Say(message string) { 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) { 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) { 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) { func (rw *ReaderWriterUi) Error(message string) {
log.Printf("ui error: %s", message) log.Printf("ui error: %s", message)
_, err := fmt.Fprint(rw.Writer, message+"\n") _, err := fmt.Fprint(rw.Writer, message+"\n")

View File

@ -17,11 +17,14 @@ func TestPrefixedUi(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
bufferUi := testUi() bufferUi := testUi()
prefixUi := &PrefixedUi{"mitchell", bufferUi} prefixUi := &PrefixedUi{"mitchell", "bar", bufferUi}
prefixUi.Say("foo") prefixUi.Say("foo")
assert.Equal(readWriter(bufferUi), "mitchell: foo\n", "should have prefix") 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") prefixUi.Error("bar")
assert.Equal(readWriter(bufferUi), "mitchell: bar\n", "should have prefix") assert.Equal(readWriter(bufferUi), "mitchell: bar\n", "should have prefix")
} }