packer: Ui has Error method

This commit is contained in:
Mitchell Hashimoto 2013-05-08 15:12:48 -07:00
parent a803af7016
commit 95153413a6
5 changed files with 43 additions and 2 deletions

View File

@ -9,7 +9,7 @@ type Command byte
func (Command) Run(env packer.Environment, args []string) int {
if len(args) != 1 {
// TODO: Error message
env.Ui().Error("A single template argument is required.\n")
return 1
}

View File

@ -22,11 +22,23 @@ type UiSayArgs struct {
Vars []interface{}
}
func (u *Ui) Error(format string, a ...interface{}) {
args := &UiSayArgs{format, a}
u.client.Call("Ui.Error", args, new(interface{}))
}
func (u *Ui) Say(format string, a ...interface{}) {
args := &UiSayArgs{format, a}
u.client.Call("Ui.Say", args, new(interface{}))
}
func (u *UiServer) Error(args *UiSayArgs, reply *interface{}) error {
u.ui.Error(args.Format, args.Vars...)
*reply = nil
return nil
}
func (u *UiServer) Say(args *UiSayArgs, reply *interface{}) error {
u.ui.Say(args.Format, args.Vars...)

View File

@ -7,11 +7,20 @@ import (
)
type testUi struct {
errorCalled bool
errorFormat string
errorVars []interface{}
sayCalled bool
sayFormat string
sayVars []interface{}
}
func (u *testUi) Error(format string, a ...interface{}) {
u.errorCalled = true
u.errorFormat = format
u.errorVars = a
}
func (u *testUi) Say(format string, a ...interface{}) {
u.sayCalled = true
u.sayFormat = format
@ -36,7 +45,10 @@ func TestUiRPC(t *testing.T) {
}
uiClient := &Ui{client}
uiClient.Say("format", "arg0", 42)
uiClient.Error("format", "arg0", 42)
assert.Equal(ui.errorFormat, "format", "format should be correct")
uiClient.Say("format", "arg0", 42)
assert.Equal(ui.sayFormat, "format", "format should be correct")
}

View File

@ -8,6 +8,7 @@ import "io"
// is formatted and various levels of output.
type Ui interface {
Say(format string, a ...interface{})
Error(format string, a...interface{})
}
// The ReaderWriterUi is a UI that writes and reads from standard Go
@ -20,3 +21,7 @@ type ReaderWriterUi struct {
func (rw *ReaderWriterUi) Say(format string, a ...interface{}) {
fmt.Fprintf(rw.Writer, format, a...)
}
func (rw *ReaderWriterUi) Error(format string, a ...interface{}) {
fmt.Fprintf(rw.Writer, format, a...)
}

View File

@ -13,6 +13,18 @@ func testUi() *ReaderWriterUi {
}
}
func TestReaderWriterUi_Error(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
bufferUi := testUi()
bufferUi.Error("foo")
assert.Equal(readWriter(bufferUi), "foo", "basic output")
bufferUi.Error("%d", 5)
assert.Equal(readWriter(bufferUi), "5", "formatting")
}
func TestReaderWriterUi_Say(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)