Commit 95153413 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Ui has Error method

parent a803af70
...@@ -9,7 +9,7 @@ type Command byte ...@@ -9,7 +9,7 @@ type Command byte
func (Command) Run(env packer.Environment, args []string) int { func (Command) Run(env packer.Environment, args []string) int {
if len(args) != 1 { if len(args) != 1 {
// TODO: Error message env.Ui().Error("A single template argument is required.\n")
return 1 return 1
} }
......
...@@ -22,11 +22,23 @@ type UiSayArgs struct { ...@@ -22,11 +22,23 @@ type UiSayArgs struct {
Vars []interface{} 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{}) { func (u *Ui) Say(format string, a ...interface{}) {
args := &UiSayArgs{format, a} args := &UiSayArgs{format, a}
u.client.Call("Ui.Say", args, new(interface{})) 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 { func (u *UiServer) Say(args *UiSayArgs, reply *interface{}) error {
u.ui.Say(args.Format, args.Vars...) u.ui.Say(args.Format, args.Vars...)
......
...@@ -7,11 +7,20 @@ import ( ...@@ -7,11 +7,20 @@ import (
) )
type testUi struct { type testUi struct {
errorCalled bool
errorFormat string
errorVars []interface{}
sayCalled bool sayCalled bool
sayFormat string sayFormat string
sayVars []interface{} 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{}) { func (u *testUi) Say(format string, a ...interface{}) {
u.sayCalled = true u.sayCalled = true
u.sayFormat = format u.sayFormat = format
...@@ -36,7 +45,10 @@ func TestUiRPC(t *testing.T) { ...@@ -36,7 +45,10 @@ func TestUiRPC(t *testing.T) {
} }
uiClient := &Ui{client} 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") assert.Equal(ui.sayFormat, "format", "format should be correct")
} }
...@@ -8,6 +8,7 @@ import "io" ...@@ -8,6 +8,7 @@ import "io"
// is formatted and various levels of output. // is formatted and various levels of output.
type Ui interface { type Ui interface {
Say(format string, a ...interface{}) Say(format string, a ...interface{})
Error(format string, a...interface{})
} }
// The ReaderWriterUi is a UI that writes and reads from standard Go // The ReaderWriterUi is a UI that writes and reads from standard Go
...@@ -20,3 +21,7 @@ type ReaderWriterUi struct { ...@@ -20,3 +21,7 @@ type ReaderWriterUi struct {
func (rw *ReaderWriterUi) Say(format string, a ...interface{}) { func (rw *ReaderWriterUi) Say(format string, a ...interface{}) {
fmt.Fprintf(rw.Writer, format, a...) fmt.Fprintf(rw.Writer, format, a...)
} }
func (rw *ReaderWriterUi) Error(format string, a ...interface{}) {
fmt.Fprintf(rw.Writer, format, a...)
}
...@@ -13,6 +13,18 @@ func testUi() *ReaderWriterUi { ...@@ -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) { func TestReaderWriterUi_Say(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