github.com/jerryclinesmith/packer@v0.3.7/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  }
    14  
    15  // UiServer wraps a packer.Ui implementation and makes it exportable
    16  // as part of a Golang RPC server.
    17  type UiServer struct {
    18  	ui packer.Ui
    19  }
    20  
    21  // The arguments sent to Ui.Machine
    22  type UiMachineArgs struct {
    23  	Category string
    24  	Args     []string
    25  }
    26  
    27  func (u *Ui) Ask(query string) (result string, err error) {
    28  	err = u.client.Call("Ui.Ask", query, &result)
    29  	return
    30  }
    31  
    32  func (u *Ui) Error(message string) {
    33  	if err := u.client.Call("Ui.Error", message, new(interface{})); err != nil {
    34  		log.Printf("Error in Ui RPC call: %s", err)
    35  	}
    36  }
    37  
    38  func (u *Ui) Machine(t string, args ...string) {
    39  	rpcArgs := &UiMachineArgs{
    40  		Category: t,
    41  		Args:     args,
    42  	}
    43  
    44  	if err := u.client.Call("Ui.Machine", rpcArgs, new(interface{})); err != nil {
    45  		log.Printf("Error in Ui RPC call: %s", err)
    46  	}
    47  }
    48  
    49  func (u *Ui) Message(message string) {
    50  	if err := u.client.Call("Ui.Message", message, new(interface{})); err != nil {
    51  		log.Printf("Error in Ui RPC call: %s", err)
    52  	}
    53  }
    54  
    55  func (u *Ui) Say(message string) {
    56  	if err := u.client.Call("Ui.Say", message, new(interface{})); err != nil {
    57  		log.Printf("Error in Ui RPC call: %s", err)
    58  	}
    59  }
    60  
    61  func (u *UiServer) Ask(query string, reply *string) (err error) {
    62  	*reply, err = u.ui.Ask(query)
    63  	return
    64  }
    65  
    66  func (u *UiServer) Error(message *string, reply *interface{}) error {
    67  	u.ui.Error(*message)
    68  
    69  	*reply = nil
    70  	return nil
    71  }
    72  
    73  func (u *UiServer) Machine(args *UiMachineArgs, reply *interface{}) error {
    74  	u.ui.Machine(args.Category, args.Args...)
    75  
    76  	*reply = nil
    77  	return nil
    78  }
    79  
    80  func (u *UiServer) Message(message *string, reply *interface{}) error {
    81  	u.ui.Message(*message)
    82  	*reply = nil
    83  	return nil
    84  }
    85  
    86  func (u *UiServer) Say(message *string, reply *interface{}) error {
    87  	u.ui.Say(*message)
    88  
    89  	*reply = nil
    90  	return nil
    91  }