github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/ui.go (about)

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