From 2788d29bd1d139bd4a344d833e3bf8c2785ede3b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 27 May 2013 15:12:48 -0700 Subject: [PATCH] packer, packer/rpc: Update Ui to just take a message --- packer/environment.go | 2 +- packer/rpc/build_test.go | 2 +- packer/rpc/builder_test.go | 2 +- packer/rpc/environment_test.go | 2 +- packer/rpc/ui.go | 38 +++++++--------------------------- packer/rpc/ui_test.go | 29 +++++++++----------------- packer/ui.go | 26 +++++++++++------------ packer/ui_test.go | 4 ++-- packer/version.go | 4 +++- 9 files changed, 39 insertions(+), 70 deletions(-) diff --git a/packer/environment.go b/packer/environment.go index 396dcf9e9..cb761e522 100644 --- a/packer/environment.go +++ b/packer/environment.go @@ -227,7 +227,7 @@ func (e *coreEnvironment) printHelp() { key = fmt.Sprintf("%v%v", key, strings.Repeat(" ", maxKeyLen-len(key))) // Output the command and the synopsis - e.ui.Say(" %v %v\n", key, synopsis) + e.ui.Say(fmt.Sprintf(" %v %v\n", key, synopsis)) } } diff --git a/packer/rpc/build_test.go b/packer/rpc/build_test.go index a163b27e9..371093e6f 100644 --- a/packer/rpc/build_test.go +++ b/packer/rpc/build_test.go @@ -68,7 +68,7 @@ func TestBuildRPC(t *testing.T) { if b.runCalled { b.runUi.Say("format") assert.True(ui.sayCalled, "say should be called") - assert.Equal(ui.sayFormat, "format", "format should be correct") + assert.Equal(ui.sayMessage, "format", "message should be correct") } } diff --git a/packer/rpc/builder_test.go b/packer/rpc/builder_test.go index be36b5aae..cd91f402f 100644 --- a/packer/rpc/builder_test.go +++ b/packer/rpc/builder_test.go @@ -64,7 +64,7 @@ func TestBuilderRPC(t *testing.T) { b.runUi.Say("format") assert.True(ui.sayCalled, "say should be called") - assert.Equal(ui.sayFormat, "format", "format should be correct") + assert.Equal(ui.sayMessage, "format", "message should be correct") assert.Equal(artifact.Id(), testBuilderArtifact.Id(), "should have artifact Id") } diff --git a/packer/rpc/environment_test.go b/packer/rpc/environment_test.go index b2ab986f2..c32e26d3e 100644 --- a/packer/rpc/environment_test.go +++ b/packer/rpc/environment_test.go @@ -94,7 +94,7 @@ func TestEnvironmentRPC(t *testing.T) { // Test calls on the Ui ui.Say("format") assert.True(testEnvUi.sayCalled, "Say should be called") - assert.Equal(testEnvUi.sayFormat, "format", "format should match") + assert.Equal(testEnvUi.sayMessage, "format", "message should match") } func TestEnvironment_ImplementsEnvironment(t *testing.T) { diff --git a/packer/rpc/ui.go b/packer/rpc/ui.go index e196c2fdf..0077b02c9 100644 --- a/packer/rpc/ui.go +++ b/packer/rpc/ui.go @@ -17,49 +17,27 @@ type UiServer struct { ui packer.Ui } -type UiSayArgs struct { - Format string - Vars []interface{} -} - -func (u *Ui) Error(format string, a ...interface{}) { - u.processArgs(a) - - args := &UiSayArgs{format, a} - if err := u.client.Call("Ui.Error", args, new(interface{})); err != nil { +func (u *Ui) Error(message string) { + if err := u.client.Call("Ui.Error", message, new(interface{})); err != nil { panic(err) } } -func (u *Ui) Say(format string, a ...interface{}) { - u.processArgs(a) - - args := &UiSayArgs{format, a} - if err := u.client.Call("Ui.Say", args, new(interface{})); err != nil { +func (u *Ui) Say(message string) { + if err := u.client.Call("Ui.Say", message, new(interface{})); err != nil { panic(err) } } -func (u *Ui) processArgs(a []interface{}) { - // We do some processing to turn certain types into more gob-friendly - // types so that some things that users expect to do just work. - for i, v := range a { - // Turn errors into strings - if err, ok := v.(error); ok { - a[i] = err.Error() - } - } -} - -func (u *UiServer) Error(args *UiSayArgs, reply *interface{}) error { - u.ui.Error(args.Format, args.Vars...) +func (u *UiServer) Error(message *string, reply *interface{}) error { + u.ui.Error(*message) *reply = nil return nil } -func (u *UiServer) Say(args *UiSayArgs, reply *interface{}) error { - u.ui.Say(args.Format, args.Vars...) +func (u *UiServer) Say(message *string, reply *interface{}) error { + u.ui.Say(*message) *reply = nil return nil diff --git a/packer/rpc/ui_test.go b/packer/rpc/ui_test.go index 306b1dc40..bb489a50d 100644 --- a/packer/rpc/ui_test.go +++ b/packer/rpc/ui_test.go @@ -2,30 +2,25 @@ package rpc import ( "cgl.tideland.biz/asserts" - "errors" "net/rpc" "testing" ) type testUi struct { errorCalled bool - errorFormat string - errorVars []interface{} + errorMessage string sayCalled bool - sayFormat string - sayVars []interface{} + sayMessage string } -func (u *testUi) Error(format string, a ...interface{}) { +func (u *testUi) Error(message string) { u.errorCalled = true - u.errorFormat = format - u.errorVars = a + u.errorMessage = message } -func (u *testUi) Say(format string, a ...interface{}) { +func (u *testUi) Say(message string) { u.sayCalled = true - u.sayFormat = format - u.sayVars = a + u.sayMessage = message } func TestUiRPC(t *testing.T) { @@ -48,13 +43,9 @@ func TestUiRPC(t *testing.T) { uiClient := &Ui{client} // Basic error and say tests - uiClient.Error("format", "arg0", 42) - assert.Equal(ui.errorFormat, "format", "format should be correct") + uiClient.Error("message") + assert.Equal(ui.errorMessage, "message", "message should be correct") - uiClient.Say("format", "arg0", 42) - assert.Equal(ui.sayFormat, "format", "format should be correct") - - // Test that errors are properly converted to strings - uiClient.Say("format", errors.New("foo")) - assert.Equal(ui.sayVars, []interface{}{"foo"}, "should have correct vars") + uiClient.Say("message") + assert.Equal(ui.sayMessage, "message", "message should be correct") } diff --git a/packer/ui.go b/packer/ui.go index 6bc7af987..313c676e2 100644 --- a/packer/ui.go +++ b/packer/ui.go @@ -10,8 +10,8 @@ import ( // world. This sort of control allows us to strictly control how output // is formatted and various levels of output. type Ui interface { - Say(format string, a ...interface{}) - Error(format string, a ...interface{}) + Say(string) + Error(string) } // PrefixedUi is a UI that wraps another UI implementation and adds a @@ -28,27 +28,25 @@ type ReaderWriterUi struct { Writer io.Writer } -func (u *PrefixedUi) Say(format string, a ...interface{}) { - u.Ui.Say(fmt.Sprintf("%s: %s", u.Prefix, format), a...) +func (u *PrefixedUi) Say(message string) { + u.Ui.Say(fmt.Sprintf("%s: %s", u.Prefix, message)) } -func (u *PrefixedUi) Error(format string, a ...interface{}) { - u.Ui.Error(fmt.Sprintf("%s: %s", u.Prefix, format), a...) +func (u *PrefixedUi) Error(message string) { + u.Ui.Error(fmt.Sprintf("%s: %s", u.Prefix, message)) } -func (rw *ReaderWriterUi) Say(format string, a ...interface{}) { - output := fmt.Sprintf(format, a...) - log.Printf("ui: %s", output) - _, err := fmt.Fprint(rw.Writer, output+"\n") +func (rw *ReaderWriterUi) Say(message string) { + log.Printf("ui: %s", message) + _, err := fmt.Fprint(rw.Writer, message+"\n") if err != nil { panic(err) } } -func (rw *ReaderWriterUi) Error(format string, a ...interface{}) { - output := fmt.Sprintf(format, a...) - log.Printf("ui error: %s", output) - _, err := fmt.Fprint(rw.Writer, output+"\n") +func (rw *ReaderWriterUi) Error(message string) { + log.Printf("ui error: %s", message) + _, err := fmt.Fprint(rw.Writer, message+"\n") if err != nil { panic(err) } diff --git a/packer/ui_test.go b/packer/ui_test.go index 7faaf9795..46f11a338 100644 --- a/packer/ui_test.go +++ b/packer/ui_test.go @@ -50,7 +50,7 @@ func TestReaderWriterUi_Error(t *testing.T) { bufferUi.Error("foo") assert.Equal(readWriter(bufferUi), "foo\n", "basic output") - bufferUi.Error("%d", 5) + bufferUi.Error("5") assert.Equal(readWriter(bufferUi), "5\n", "formatting") } @@ -62,7 +62,7 @@ func TestReaderWriterUi_Say(t *testing.T) { bufferUi.Say("foo") assert.Equal(readWriter(bufferUi), "foo\n", "basic output") - bufferUi.Say("%d", 5) + bufferUi.Say("5") assert.Equal(readWriter(bufferUi), "5\n", "formatting") } diff --git a/packer/version.go b/packer/version.go index b9d2e1105..3733cef30 100644 --- a/packer/version.go +++ b/packer/version.go @@ -1,5 +1,7 @@ package packer +import "fmt" + // The version of packer. const Version = "0.1.0.dev" @@ -7,7 +9,7 @@ type versionCommand byte // Implement the Command interface by simply showing the version func (versionCommand) Run(env Environment, args []string) int { - env.Ui().Say("Packer v%v\n", Version) + env.Ui().Say(fmt.Sprintf("Packer v%v\n", Version)) return 0 }