packer: Add Machine func to Ui

This commit is contained in:
Mitchell Hashimoto 2013-08-11 18:16:00 -07:00
parent 7004813a1f
commit fb6d2754da
4 changed files with 51 additions and 0 deletions

View File

@ -17,6 +17,12 @@ type UiServer struct {
ui packer.Ui
}
// The arguments sent to Ui.Machine
type UiMachineArgs struct {
category string
args []string
}
func (u *Ui) Ask(query string) (result string, err error) {
err = u.client.Call("Ui.Ask", query, &result)
return
@ -28,6 +34,17 @@ func (u *Ui) Error(message string) {
}
}
func (u *Ui) Machine(t string, args ...string) {
rpcArgs := &UiMachineArgs{
category: t,
args: args,
}
if err := u.client.Call("Ui.Message", rpcArgs, new(interface{})); err != nil {
panic(err)
}
}
func (u *Ui) Message(message string) {
if err := u.client.Call("Ui.Message", message, new(interface{})); err != nil {
panic(err)
@ -52,6 +69,13 @@ func (u *UiServer) Error(message *string, reply *interface{}) error {
return nil
}
func (u *UiServer) Machine(args *UiMachineArgs, reply *interface{}) error {
u.ui.Machine(args.category, args.args...)
*reply = nil
return nil
}
func (u *UiServer) Message(message *string, reply *interface{}) error {
u.ui.Message(*message)
*reply = nil

View File

@ -11,6 +11,9 @@ type testUi struct {
askQuery string
errorCalled bool
errorMessage string
machineCalled bool
machineType string
machineArgs []string
messageCalled bool
messageMessage string
sayCalled bool
@ -28,6 +31,12 @@ func (u *testUi) Error(message string) {
u.errorMessage = message
}
func (u *testUi) Machine(t string, args ...string) {
u.machineCalled = true
u.machineType = t
u.machineArgs = args
}
func (u *testUi) Message(message string) {
u.messageCalled = true
u.messageMessage = message

View File

@ -33,6 +33,7 @@ type Ui interface {
Say(string)
Message(string)
Error(string)
Machine(string, ...string)
}
// ColoredUi is a UI that is colored using terminal colors.
@ -80,6 +81,11 @@ func (u *ColoredUi) Error(message string) {
u.Ui.Error(u.colorize(message, color, true))
}
func (u *ColoredUi) Machine(t string, args ...string) {
// Don't colorize machine-readable output
u.Ui.Machine(t, args...)
}
func (u *ColoredUi) colorize(message string, color UiColor, bold bool) string {
if !u.supportsColors() {
return message
@ -123,6 +129,11 @@ func (u *PrefixedUi) Error(message string) {
u.Ui.Error(u.prefixLines(u.SayPrefix, message))
}
func (u *PrefixedUi) Machine(t string, args ...string) {
// Just pass it through for now.
u.Ui.Machine(t, args...)
}
func (u *PrefixedUi) prefixLines(prefix, message string) string {
var result bytes.Buffer
@ -209,3 +220,7 @@ func (rw *ReaderWriterUi) Error(message string) {
panic(err)
}
}
func (rw *ReaderWriterUi) Machine(t string, args ...string) {
// TODO
}

View File

@ -106,6 +106,9 @@ func (su *stubUi) Ask(string) (string, error) {
func (su *stubUi) Error(string) {
}
func (su *stubUi) Machine(string, ...string) {
}
func (su *stubUi) Message(string) {
}