From 02edc7579b32af7517c7f9fc789577af87f92be8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 11 Aug 2013 18:20:27 -0700 Subject: [PATCH] packer: Rename ReaderWriterUi to BasicUi --- command/build/command_test.go | 2 +- packer/communicator_test.go | 2 +- packer/environment.go | 2 +- packer/environment_test.go | 10 +++++----- packer/ui.go | 19 +++++++++++-------- packer/ui_test.go | 16 ++++++++-------- 6 files changed, 27 insertions(+), 24 deletions(-) diff --git a/command/build/command_test.go b/command/build/command_test.go index 433f0ef46..33559cfc5 100644 --- a/command/build/command_test.go +++ b/command/build/command_test.go @@ -9,7 +9,7 @@ import ( func testEnvironment() packer.Environment { config := packer.DefaultEnvironmentConfig() - config.Ui = &packer.ReaderWriterUi{ + config.Ui = &packer.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } diff --git a/packer/communicator_test.go b/packer/communicator_test.go index d9f31c2bd..ac4c5b3e8 100644 --- a/packer/communicator_test.go +++ b/packer/communicator_test.go @@ -47,7 +47,7 @@ func TestRemoteCmd_StartWithUi(t *testing.T) { Stdout: rcOutput, } - testUi := &ReaderWriterUi{ + testUi := &BasicUi{ Reader: new(bytes.Buffer), Writer: uiOutput, } diff --git a/packer/environment.go b/packer/environment.go index b0ee2579f..267db5826 100644 --- a/packer/environment.go +++ b/packer/environment.go @@ -73,7 +73,7 @@ type EnvironmentConfig struct { func DefaultEnvironmentConfig() *EnvironmentConfig { config := &EnvironmentConfig{} config.Commands = make([]string, 0) - config.Ui = &ReaderWriterUi{ + config.Ui = &BasicUi{ Reader: os.Stdin, Writer: os.Stdout, } diff --git a/packer/environment_test.go b/packer/environment_test.go index 125058bf2..3a15edea7 100644 --- a/packer/environment_test.go +++ b/packer/environment_test.go @@ -19,7 +19,7 @@ func init() { func testEnvironment() Environment { config := DefaultEnvironmentConfig() - config.Ui = &ReaderWriterUi{ + config.Ui = &BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } @@ -45,8 +45,8 @@ func TestEnvironment_DefaultConfig_Ui(t *testing.T) { config := DefaultEnvironmentConfig() assert.NotNil(config.Ui, "default UI should not be nil") - rwUi, ok := config.Ui.(*ReaderWriterUi) - assert.True(ok, "default UI should be ReaderWriterUi") + rwUi, ok := config.Ui.(*BasicUi) + assert.True(ok, "default UI should be BasicUi") assert.Equal(rwUi.Writer, os.Stdout, "default UI should go to stdout") assert.Equal(rwUi.Reader, os.Stdin, "default UI should read from stdin") } @@ -175,7 +175,7 @@ func TestEnvironment_DefaultCli_Help(t *testing.T) { // A little lambda to help us test the output actually contains help testOutput := func() { - buffer := defaultEnv.Ui().(*ReaderWriterUi).Writer.(*bytes.Buffer) + buffer := defaultEnv.Ui().(*BasicUi).Writer.(*bytes.Buffer) output := buffer.String() buffer.Reset() assert.True(strings.Contains(output, "usage: packer"), "should print help") @@ -341,7 +341,7 @@ func TestEnvironmentProvisioner_Error(t *testing.T) { func TestEnvironment_SettingUi(t *testing.T) { assert := asserts.NewTestingAsserts(t, true) - ui := &ReaderWriterUi{ + ui := &BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } diff --git a/packer/ui.go b/packer/ui.go index 5d5ca1065..d6c97f355 100644 --- a/packer/ui.go +++ b/packer/ui.go @@ -51,9 +51,12 @@ type PrefixedUi struct { Ui Ui } -// The ReaderWriterUi is a UI that writes and reads from standard Go -// io.Reader and io.Writer. -type ReaderWriterUi struct { +// The BasicUI is a UI that reads and writes from a standard Go reader +// and writer. It is safe to be called from multiple goroutines. The +// target for machine-readable output can be configured by prefixing the +// type of the machine readable output with the target and separating it +// with a comma. +type BasicUi struct { Reader io.Reader Writer io.Writer l sync.Mutex @@ -144,7 +147,7 @@ func (u *PrefixedUi) prefixLines(prefix, message string) string { return strings.TrimRightFunc(result.String(), unicode.IsSpace) } -func (rw *ReaderWriterUi) Ask(query string) (string, error) { +func (rw *BasicUi) Ask(query string) (string, error) { rw.l.Lock() defer rw.l.Unlock() @@ -188,7 +191,7 @@ func (rw *ReaderWriterUi) Ask(query string) (string, error) { } } -func (rw *ReaderWriterUi) Say(message string) { +func (rw *BasicUi) Say(message string) { rw.l.Lock() defer rw.l.Unlock() @@ -199,7 +202,7 @@ func (rw *ReaderWriterUi) Say(message string) { } } -func (rw *ReaderWriterUi) Message(message string) { +func (rw *BasicUi) Message(message string) { rw.l.Lock() defer rw.l.Unlock() @@ -210,7 +213,7 @@ func (rw *ReaderWriterUi) Message(message string) { } } -func (rw *ReaderWriterUi) Error(message string) { +func (rw *BasicUi) Error(message string) { rw.l.Lock() defer rw.l.Unlock() @@ -221,6 +224,6 @@ func (rw *ReaderWriterUi) Error(message string) { } } -func (rw *ReaderWriterUi) Machine(t string, args ...string) { +func (rw *BasicUi) Machine(t string, args ...string) { // TODO } diff --git a/packer/ui_test.go b/packer/ui_test.go index 6a48a80a3..b42255f15 100644 --- a/packer/ui_test.go +++ b/packer/ui_test.go @@ -6,8 +6,8 @@ import ( "testing" ) -func testUi() *ReaderWriterUi { - return &ReaderWriterUi{ +func testUi() *BasicUi { + return &BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer), } @@ -71,15 +71,15 @@ func TestPrefixedUi_ImplUi(t *testing.T) { } } -func TestReaderWriterUi_ImplUi(t *testing.T) { +func TestBasicUi_ImplUi(t *testing.T) { var raw interface{} - raw = &ReaderWriterUi{} + raw = &BasicUi{} if _, ok := raw.(Ui); !ok { - t.Fatalf("ReaderWriterUi must implement Ui") + t.Fatalf("BasicUi must implement Ui") } } -func TestReaderWriterUi_Error(t *testing.T) { +func TestBasicUi_Error(t *testing.T) { assert := asserts.NewTestingAsserts(t, true) bufferUi := testUi() @@ -91,7 +91,7 @@ func TestReaderWriterUi_Error(t *testing.T) { assert.Equal(readWriter(bufferUi), "5\n", "formatting") } -func TestReaderWriterUi_Say(t *testing.T) { +func TestBasicUi_Say(t *testing.T) { assert := asserts.NewTestingAsserts(t, true) bufferUi := testUi() @@ -105,7 +105,7 @@ func TestReaderWriterUi_Say(t *testing.T) { // This reads the output from the bytes.Buffer in our test object // and then resets the buffer. -func readWriter(ui *ReaderWriterUi) (result string) { +func readWriter(ui *BasicUi) (result string) { buffer := ui.Writer.(*bytes.Buffer) result = buffer.String() buffer.Reset()