vitess.io/vitess@v0.16.2/go/vt/vtctl/vtctlclient/interface.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package vtctlclient contains the generic client side of the remote vtctl protocol. 18 package vtctlclient 19 20 import ( 21 "context" 22 "fmt" 23 "time" 24 25 "github.com/spf13/pflag" 26 27 "vitess.io/vitess/go/vt/log" 28 "vitess.io/vitess/go/vt/logutil" 29 "vitess.io/vitess/go/vt/servenv" 30 ) 31 32 // vtctlClientProtocol specifics which RPC client implementation should be used. 33 var vtctlClientProtocol = "grpc" 34 35 func RegisterFlags(fs *pflag.FlagSet) { 36 fs.StringVar(&vtctlClientProtocol, "vtctl_client_protocol", vtctlClientProtocol, "Protocol to use to talk to the vtctl server.") 37 } 38 39 func init() { 40 for _, cmd := range []string{ 41 "vtctlclient", 42 "vttestserver", 43 } { 44 servenv.OnParseFor(cmd, RegisterFlags) 45 } 46 } 47 48 // VtctlClient defines the interface used to send remote vtctl commands 49 type VtctlClient interface { 50 // ExecuteVtctlCommand will execute the command remotely 51 ExecuteVtctlCommand(ctx context.Context, args []string, actionTimeout time.Duration) (logutil.EventStream, error) 52 53 // Close will terminate the connection. This object won't be 54 // used after this. 55 Close() 56 } 57 58 // Factory functions are registered by client implementations 59 type Factory func(addr string) (VtctlClient, error) 60 61 var factories = make(map[string]Factory) 62 63 // RegisterFactory allows a client implementation to register itself. 64 func RegisterFactory(name string, factory Factory) { 65 if _, ok := factories[name]; ok { 66 log.Fatalf("RegisterFactory: %s already exists", name) 67 } 68 factories[name] = factory 69 } 70 71 // UnregisterFactoryForTest allows to unregister a client implementation from the static map. 72 // This function is used by unit tests to cleanly unregister any fake implementations. 73 // This way, a test package can use the same name for different fakes and no dangling fakes are 74 // left behind in the static factories map after the test. 75 func UnregisterFactoryForTest(name string) { 76 if _, ok := factories[name]; !ok { 77 log.Fatalf("UnregisterFactoryForTest: %s is not registered", name) 78 } 79 delete(factories, name) 80 } 81 82 // New allows a user of the client library to get its implementation. 83 func New(addr string) (VtctlClient, error) { 84 factory, ok := factories[vtctlClientProtocol] 85 if !ok { 86 return nil, fmt.Errorf("unknown vtctl client protocol: %v", vtctlClientProtocol) 87 } 88 return factory(addr) 89 }