packer-cn/packer/rpc/ui.go

97 lines
2.0 KiB
Go
Raw Normal View History

2013-05-03 18:49:15 -04:00
package rpc
import (
"log"
2013-05-03 18:49:15 -04:00
"net/rpc"
2018-01-22 20:21:10 -05:00
"github.com/hashicorp/packer/packer"
2013-05-03 18:49:15 -04:00
)
// An implementation of packer.Ui where the Ui is actually executed
// over an RPC connection.
type Ui struct {
client *rpc.Client
endpoint string
2013-05-03 18:49:15 -04:00
}
var _ packer.Ui = new(Ui)
2013-05-03 18:49:15 -04:00
// UiServer wraps a packer.Ui implementation and makes it exportable
// as part of a Golang RPC server.
type UiServer struct {
ui packer.Ui
register func(name string, rcvr interface{}) error
2013-05-03 18:49:15 -04:00
}
2013-08-11 21:16:00 -04:00
// The arguments sent to Ui.Machine
type UiMachineArgs struct {
Category string
Args []string
2013-08-11 21:16:00 -04:00
}
func (u *Ui) Ask(query string) (result string, err error) {
err = u.client.Call("Ui.Ask", query, &result)
return
}
func (u *Ui) Error(message string) {
if err := u.client.Call("Ui.Error", message, new(interface{})); err != nil {
2018-11-09 08:44:34 -05:00
log.Printf("Error in Ui.Error RPC call: %s", err)
}
2013-05-08 18:12:48 -04:00
}
2013-08-11 21:16:00 -04:00
func (u *Ui) Machine(t string, args ...string) {
rpcArgs := &UiMachineArgs{
Category: t,
Args: args,
2013-08-11 21:16:00 -04:00
}
if err := u.client.Call("Ui.Machine", rpcArgs, new(interface{})); err != nil {
2018-11-09 08:44:34 -05:00
log.Printf("Error in Ui.Machine RPC call: %s", err)
2013-08-11 21:16:00 -04:00
}
}
2013-06-03 14:30:38 -04:00
func (u *Ui) Message(message string) {
if err := u.client.Call("Ui.Message", message, new(interface{})); err != nil {
2018-11-09 08:44:34 -05:00
log.Printf("Error in Ui.Message RPC call: %s", err)
2013-06-03 14:30:38 -04:00
}
}
func (u *Ui) Say(message string) {
if err := u.client.Call("Ui.Say", message, new(interface{})); err != nil {
2018-11-09 08:44:34 -05:00
log.Printf("Error in Ui.Say RPC call: %s", err)
}
}
func (u *UiServer) Ask(query string, reply *string) (err error) {
*reply, err = u.ui.Ask(query)
return
}
func (u *UiServer) Error(message *string, reply *interface{}) error {
u.ui.Error(*message)
2013-05-08 18:12:48 -04:00
*reply = nil
return nil
}
2013-08-11 21:16:00 -04:00
func (u *UiServer) Machine(args *UiMachineArgs, reply *interface{}) error {
u.ui.Machine(args.Category, args.Args...)
2013-08-11 21:16:00 -04:00
*reply = nil
return nil
}
2013-06-03 14:30:38 -04:00
func (u *UiServer) Message(message *string, reply *interface{}) error {
u.ui.Message(*message)
*reply = nil
return nil
}
func (u *UiServer) Say(message *string, reply *interface{}) error {
u.ui.Say(*message)
2013-05-03 18:49:15 -04:00
*reply = nil
return nil
}