github.phpd.cn/hashicorp/packer@v1.3.2/packer/rpc/ui.go (about) 1 package rpc 2 3 import ( 4 "io" 5 "log" 6 "net/rpc" 7 8 "github.com/hashicorp/packer/packer" 9 ) 10 11 // An implementation of packer.Ui where the Ui is actually executed 12 // over an RPC connection. 13 type Ui struct { 14 client *rpc.Client 15 endpoint string 16 } 17 18 var _ packer.Ui = new(Ui) 19 20 // UiServer wraps a packer.Ui implementation and makes it exportable 21 // as part of a Golang RPC server. 22 type UiServer struct { 23 ui packer.Ui 24 register func(name string, rcvr interface{}) error 25 } 26 27 // The arguments sent to Ui.Machine 28 type UiMachineArgs struct { 29 Category string 30 Args []string 31 } 32 33 func (u *Ui) Ask(query string) (result string, err error) { 34 err = u.client.Call("Ui.Ask", query, &result) 35 return 36 } 37 38 func (u *Ui) Error(message string) { 39 if err := u.client.Call("Ui.Error", message, new(interface{})); err != nil { 40 log.Printf("Error in Ui RPC call: %s", err) 41 } 42 } 43 44 func (u *Ui) Machine(t string, args ...string) { 45 rpcArgs := &UiMachineArgs{ 46 Category: t, 47 Args: args, 48 } 49 50 if err := u.client.Call("Ui.Machine", rpcArgs, new(interface{})); err != nil { 51 log.Printf("Error in Ui RPC call: %s", err) 52 } 53 } 54 55 func (u *Ui) Message(message string) { 56 if err := u.client.Call("Ui.Message", message, new(interface{})); err != nil { 57 log.Printf("Error in Ui RPC call: %s", err) 58 } 59 } 60 61 func (u *Ui) Say(message string) { 62 if err := u.client.Call("Ui.Say", message, new(interface{})); err != nil { 63 log.Printf("Error in Ui RPC call: %s", err) 64 } 65 } 66 67 func (u *Ui) ProgressBar() packer.ProgressBar { 68 if err := u.client.Call("Ui.ProgressBar", new(interface{}), new(interface{})); err != nil { 69 log.Printf("Error in Ui RPC call: %s", err) 70 } 71 return u // Ui is also a progress bar !! 72 } 73 74 var _ packer.ProgressBar = new(Ui) 75 76 func (pb *Ui) Start(total int64) { 77 pb.client.Call("Ui.Start", total, new(interface{})) 78 } 79 80 func (pb *Ui) Add(current int64) { 81 pb.client.Call("Ui.Add", current, new(interface{})) 82 } 83 84 func (pb *Ui) Finish() { 85 pb.client.Call("Ui.Finish", nil, new(interface{})) 86 } 87 88 func (pb *Ui) NewProxyReader(r io.Reader) io.Reader { 89 return &packer.ProxyReader{Reader: r, ProgressBar: pb} 90 } 91 92 func (u *UiServer) Ask(query string, reply *string) (err error) { 93 *reply, err = u.ui.Ask(query) 94 return 95 } 96 97 func (u *UiServer) Error(message *string, reply *interface{}) error { 98 u.ui.Error(*message) 99 100 *reply = nil 101 return nil 102 } 103 104 func (u *UiServer) Machine(args *UiMachineArgs, reply *interface{}) error { 105 u.ui.Machine(args.Category, args.Args...) 106 107 *reply = nil 108 return nil 109 } 110 111 func (u *UiServer) Message(message *string, reply *interface{}) error { 112 u.ui.Message(*message) 113 *reply = nil 114 return nil 115 } 116 117 func (u *UiServer) Say(message *string, reply *interface{}) error { 118 u.ui.Say(*message) 119 120 *reply = nil 121 return nil 122 } 123 124 func (u *UiServer) ProgressBar(_ *string, reply *interface{}) error { 125 // No-op for now, this function might be 126 // used in the future if we want to use 127 // different progress bars with identifiers. 128 u.ui.ProgressBar() 129 return nil 130 } 131 132 func (pb *UiServer) Finish(_ string, _ *interface{}) error { 133 pb.ui.ProgressBar().Finish() 134 return nil 135 } 136 137 func (pb *UiServer) Start(total int64, _ *interface{}) error { 138 pb.ui.ProgressBar().Start(total) 139 return nil 140 } 141 142 func (pb *UiServer) Add(current int64, _ *interface{}) error { 143 pb.ui.ProgressBar().Add(current) 144 return nil 145 }