github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/packer/rpc/ui.go (about)

     1  package rpc
     2  
     3  import (
     4  	"github.com/mitchellh/packer/packer"
     5  	"net/rpc"
     6  )
     7  
     8  // An implementation of packer.Ui where the Ui is actually executed
     9  // over an RPC connection.
    10  type Ui struct {
    11  	client *rpc.Client
    12  }
    13  
    14  // UiServer wraps a packer.Ui implementation and makes it exportable
    15  // as part of a Golang RPC server.
    16  type UiServer struct {
    17  	ui packer.Ui
    18  }
    19  
    20  // The arguments sent to Ui.Machine
    21  type UiMachineArgs struct {
    22  	Category string
    23  	Args     []string
    24  }
    25  
    26  func (u *Ui) Ask(query string) (result string, err error) {
    27  	err = u.client.Call("Ui.Ask", query, &result)
    28  	return
    29  }
    30  
    31  func (u *Ui) Error(message string) {
    32  	if err := u.client.Call("Ui.Error", message, new(interface{})); err != nil {
    33  		panic(err)
    34  	}
    35  }
    36  
    37  func (u *Ui) Machine(t string, args ...string) {
    38  	rpcArgs := &UiMachineArgs{
    39  		Category: t,
    40  		Args:     args,
    41  	}
    42  
    43  	if err := u.client.Call("Ui.Machine", rpcArgs, new(interface{})); err != nil {
    44  		panic(err)
    45  	}
    46  }
    47  
    48  func (u *Ui) Message(message string) {
    49  	if err := u.client.Call("Ui.Message", message, new(interface{})); err != nil {
    50  		panic(err)
    51  	}
    52  }
    53  
    54  func (u *Ui) Say(message string) {
    55  	if err := u.client.Call("Ui.Say", message, new(interface{})); err != nil {
    56  		panic(err)
    57  	}
    58  }
    59  
    60  func (u *UiServer) Ask(query string, reply *string) (err error) {
    61  	*reply, err = u.ui.Ask(query)
    62  	return
    63  }
    64  
    65  func (u *UiServer) Error(message *string, reply *interface{}) error {
    66  	u.ui.Error(*message)
    67  
    68  	*reply = nil
    69  	return nil
    70  }
    71  
    72  func (u *UiServer) Machine(args *UiMachineArgs, reply *interface{}) error {
    73  	u.ui.Machine(args.Category, args.Args...)
    74  
    75  	*reply = nil
    76  	return nil
    77  }
    78  
    79  func (u *UiServer) Message(message *string, reply *interface{}) error {
    80  	u.ui.Message(*message)
    81  	*reply = nil
    82  	return nil
    83  }
    84  
    85  func (u *UiServer) Say(message *string, reply *interface{}) error {
    86  	u.ui.Say(*message)
    87  
    88  	*reply = nil
    89  	return nil
    90  }