github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/packer/rpc/ui.go (about)

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