vitess.io/vitess@v0.16.2/go/vt/vtctl/vtctldclient/client.go (about) 1 // Package vtctldclient contains the generic client side of the remote vtctld 2 // protocol. 3 package vtctldclient 4 5 import ( 6 "fmt" 7 "log" 8 9 vtctlservicepb "vitess.io/vitess/go/vt/proto/vtctlservice" 10 ) 11 12 type VtctldClient interface { 13 vtctlservicepb.VtctldClient 14 15 // Close augments the vtctlservicepb.VtctlClient interface with io.Closer. 16 Close() error 17 } 18 19 // Factory is a function that creates new VtctldClients. 20 type Factory func(addr string) (VtctldClient, error) 21 22 var registry = map[string]Factory{} 23 24 // Register adds a VtctldClient factory for the given name (protocol). 25 // Attempting to register mulitple factories for the same protocol is a fatal 26 // error. 27 func Register(name string, factory Factory) { 28 if _, ok := registry[name]; ok { 29 log.Fatalf("Register: %s already registered", name) 30 } 31 32 registry[name] = factory 33 } 34 35 // New returns a VtctldClient for the given protocol, connected to a 36 // VtctldServer on the given addr. This function returns an error if no client 37 // factory was registered for the given protocol. 38 // 39 // This is a departure from vtctlclient's New, which relies on a flag in the 40 // global namespace to determine the protocol to use. Instead, we require 41 // users to specify their own flag in their own (hopefully not global) namespace 42 // to determine the protocol to pass into here. 43 func New(protocol string, addr string) (VtctldClient, error) { 44 factory, ok := registry[protocol] 45 if !ok { 46 return nil, fmt.Errorf("unknown vtctld client protocol: %s", protocol) 47 } 48 49 return factory(addr) 50 }