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() {
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))
}
}
......
......@@ -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")
}
}
......
......@@ -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")
}
......
......@@ -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) {
......
......@@ -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
......
......@@ -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")
}
......@@ -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)
}
......
......@@ -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")
}
......
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
}
......
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