packer: Ui has Error method
This commit is contained in:
parent
a803af7016
commit
95153413a6
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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...)
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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...)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue