Commit 2788d29b authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer, packer/rpc: Update Ui to just take a message

parent f726ea28
...@@ -227,7 +227,7 @@ func (e *coreEnvironment) printHelp() { ...@@ -227,7 +227,7 @@ func (e *coreEnvironment) printHelp() {
key = fmt.Sprintf("%v%v", key, strings.Repeat(" ", maxKeyLen-len(key))) key = fmt.Sprintf("%v%v", key, strings.Repeat(" ", maxKeyLen-len(key)))
// Output the command and the synopsis // Output the command and the synopsis
e.ui.Say(" %v %v\n", key, synopsis) e.ui.Say(fmt.Sprintf(" %v %v\n", key, synopsis))
} }
} }
......
...@@ -68,7 +68,7 @@ func TestBuildRPC(t *testing.T) { ...@@ -68,7 +68,7 @@ func TestBuildRPC(t *testing.T) {
if b.runCalled { if b.runCalled {
b.runUi.Say("format") b.runUi.Say("format")
assert.True(ui.sayCalled, "say should be called") 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")
} }
} }
......
...@@ -64,7 +64,7 @@ func TestBuilderRPC(t *testing.T) { ...@@ -64,7 +64,7 @@ func TestBuilderRPC(t *testing.T) {
b.runUi.Say("format") b.runUi.Say("format")
assert.True(ui.sayCalled, "say should be called") 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") assert.Equal(artifact.Id(), testBuilderArtifact.Id(), "should have artifact Id")
} }
......
...@@ -94,7 +94,7 @@ func TestEnvironmentRPC(t *testing.T) { ...@@ -94,7 +94,7 @@ func TestEnvironmentRPC(t *testing.T) {
// Test calls on the Ui // Test calls on the Ui
ui.Say("format") ui.Say("format")
assert.True(testEnvUi.sayCalled, "Say should be called") 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) { func TestEnvironment_ImplementsEnvironment(t *testing.T) {
......
...@@ -17,49 +17,27 @@ type UiServer struct { ...@@ -17,49 +17,27 @@ type UiServer struct {
ui packer.Ui ui packer.Ui
} }
type UiSayArgs struct { func (u *Ui) Error(message string) {
Format string if err := u.client.Call("Ui.Error", message, new(interface{})); err != nil {
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 {
panic(err) panic(err)
} }
} }
func (u *Ui) Say(format string, a ...interface{}) { func (u *Ui) Say(message string) {
u.processArgs(a) if err := u.client.Call("Ui.Say", message, new(interface{})); err != nil {
args := &UiSayArgs{format, a}
if err := u.client.Call("Ui.Say", args, new(interface{})); err != nil {
panic(err) panic(err)
} }
} }
func (u *Ui) processArgs(a []interface{}) { func (u *UiServer) Error(message *string, reply *interface{}) error {
// We do some processing to turn certain types into more gob-friendly u.ui.Error(*message)
// 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...)
*reply = nil *reply = nil
return nil return nil
} }
func (u *UiServer) Say(args *UiSayArgs, reply *interface{}) error { func (u *UiServer) Say(message *string, reply *interface{}) error {
u.ui.Say(args.Format, args.Vars...) u.ui.Say(*message)
*reply = nil *reply = nil
return nil return nil
......
...@@ -2,30 +2,25 @@ package rpc ...@@ -2,30 +2,25 @@ package rpc
import ( import (
"cgl.tideland.biz/asserts" "cgl.tideland.biz/asserts"
"errors"
"net/rpc" "net/rpc"
"testing" "testing"
) )
type testUi struct { type testUi struct {
errorCalled bool errorCalled bool
errorFormat string errorMessage string
errorVars []interface{}
sayCalled bool sayCalled bool
sayFormat string sayMessage string
sayVars []interface{}
} }
func (u *testUi) Error(format string, a ...interface{}) { func (u *testUi) Error(message string) {
u.errorCalled = true u.errorCalled = true
u.errorFormat = format u.errorMessage = message
u.errorVars = a
} }
func (u *testUi) Say(format string, a ...interface{}) { func (u *testUi) Say(message string) {
u.sayCalled = true u.sayCalled = true
u.sayFormat = format u.sayMessage = message
u.sayVars = a
} }
func TestUiRPC(t *testing.T) { func TestUiRPC(t *testing.T) {
...@@ -48,13 +43,9 @@ func TestUiRPC(t *testing.T) { ...@@ -48,13 +43,9 @@ func TestUiRPC(t *testing.T) {
uiClient := &Ui{client} uiClient := &Ui{client}
// Basic error and say tests // Basic error and say tests
uiClient.Error("format", "arg0", 42) uiClient.Error("message")
assert.Equal(ui.errorFormat, "format", "format should be correct") assert.Equal(ui.errorMessage, "message", "message should be correct")
uiClient.Say("format", "arg0", 42) uiClient.Say("message")
assert.Equal(ui.sayFormat, "format", "format should be correct") assert.Equal(ui.sayMessage, "message", "message 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")
} }
...@@ -10,8 +10,8 @@ import ( ...@@ -10,8 +10,8 @@ import (
// world. This sort of control allows us to strictly control how output // world. This sort of control allows us to strictly control how output
// 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(string)
Error(format string, a ...interface{}) 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
...@@ -28,27 +28,25 @@ type ReaderWriterUi struct { ...@@ -28,27 +28,25 @@ type ReaderWriterUi struct {
Writer io.Writer Writer io.Writer
} }
func (u *PrefixedUi) Say(format string, a ...interface{}) { func (u *PrefixedUi) Say(message string) {
u.Ui.Say(fmt.Sprintf("%s: %s", u.Prefix, format), a...) u.Ui.Say(fmt.Sprintf("%s: %s", u.Prefix, message))
} }
func (u *PrefixedUi) Error(format string, a ...interface{}) { func (u *PrefixedUi) Error(message string) {
u.Ui.Error(fmt.Sprintf("%s: %s", u.Prefix, format), a...) u.Ui.Error(fmt.Sprintf("%s: %s", u.Prefix, message))
} }
func (rw *ReaderWriterUi) Say(format string, a ...interface{}) { func (rw *ReaderWriterUi) Say(message string) {
output := fmt.Sprintf(format, a...) log.Printf("ui: %s", message)
log.Printf("ui: %s", output) _, err := fmt.Fprint(rw.Writer, message+"\n")
_, err := fmt.Fprint(rw.Writer, output+"\n")
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
func (rw *ReaderWriterUi) Error(format string, a ...interface{}) { func (rw *ReaderWriterUi) Error(message string) {
output := fmt.Sprintf(format, a...) log.Printf("ui error: %s", message)
log.Printf("ui error: %s", output) _, err := fmt.Fprint(rw.Writer, message+"\n")
_, err := fmt.Fprint(rw.Writer, output+"\n")
if err != nil { if err != nil {
panic(err) panic(err)
} }
......
...@@ -50,7 +50,7 @@ func TestReaderWriterUi_Error(t *testing.T) { ...@@ -50,7 +50,7 @@ func TestReaderWriterUi_Error(t *testing.T) {
bufferUi.Error("foo") bufferUi.Error("foo")
assert.Equal(readWriter(bufferUi), "foo\n", "basic output") assert.Equal(readWriter(bufferUi), "foo\n", "basic output")
bufferUi.Error("%d", 5) bufferUi.Error("5")
assert.Equal(readWriter(bufferUi), "5\n", "formatting") assert.Equal(readWriter(bufferUi), "5\n", "formatting")
} }
...@@ -62,7 +62,7 @@ func TestReaderWriterUi_Say(t *testing.T) { ...@@ -62,7 +62,7 @@ func TestReaderWriterUi_Say(t *testing.T) {
bufferUi.Say("foo") bufferUi.Say("foo")
assert.Equal(readWriter(bufferUi), "foo\n", "basic output") assert.Equal(readWriter(bufferUi), "foo\n", "basic output")
bufferUi.Say("%d", 5) bufferUi.Say("5")
assert.Equal(readWriter(bufferUi), "5\n", "formatting") assert.Equal(readWriter(bufferUi), "5\n", "formatting")
} }
......
package packer package packer
import "fmt"
// The version of packer. // The version of packer.
const Version = "0.1.0.dev" const Version = "0.1.0.dev"
...@@ -7,7 +9,7 @@ type versionCommand byte ...@@ -7,7 +9,7 @@ type versionCommand byte
// Implement the Command interface by simply showing the version // Implement the Command interface by simply showing the version
func (versionCommand) Run(env Environment, args []string) int { 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 return 0
} }
......
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