diff --git a/go/cmd/vtctldclient/main.go b/go/cmd/vtctldclient/main.go index 6220c990a9..2fcf10ecf4 100644 --- a/go/cmd/vtctldclient/main.go +++ b/go/cmd/vtctldclient/main.go @@ -28,6 +28,7 @@ import ( "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/servenv" "vitess.io/vitess/go/vt/vtctl/grpcclientcommon" + "vitess.io/vitess/go/vt/vtctl/vtctlclient" ) func main() { @@ -41,6 +42,7 @@ func main() { grpccommon.RegisterFlags(command.Root.PersistentFlags()) grpcclientcommon.RegisterFlags(command.Root.PersistentFlags()) servenv.RegisterMySQLServerFlags(command.Root.PersistentFlags()) + vtctlclient.RegisterFlags(command.Root.PersistentFlags()) // hack to get rid of an "ERROR: logging before flag.Parse" args := os.Args[:] diff --git a/go/flags/endtoend/vtctlclient.txt b/go/flags/endtoend/vtctlclient.txt index 3b58f16fd2..a73c1c3209 100644 --- a/go/flags/endtoend/vtctlclient.txt +++ b/go/flags/endtoend/vtctlclient.txt @@ -33,7 +33,7 @@ Usage of vtctlclient: -v, --v Level log level for V logs --version print binary version --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging - --vtctl_client_protocol string the protocol to use to talk to the vtctl server (default "grpc") + --vtctl_client_protocol string Protocol to use to talk to the vtctl server. (default "grpc") --vtctld_grpc_ca string the server ca to use to validate servers when connecting --vtctld_grpc_cert string the cert to use to connect --vtctld_grpc_crl string the server crl to use to validate server certificates when connecting diff --git a/go/flags/endtoend/vtctldclient.txt b/go/flags/endtoend/vtctldclient.txt index dd68b374d2..2b08fabacd 100644 --- a/go/flags/endtoend/vtctldclient.txt +++ b/go/flags/endtoend/vtctldclient.txt @@ -154,7 +154,7 @@ Flags: -v, --v Level log level for V logs --version print binary version --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging - --vtctl_client_protocol string the protocol to use to talk to the vtctl server (default "grpc") + --vtctl_client_protocol string Protocol to use to talk to the vtctl server. (default "grpc") --vtctld_grpc_ca string the server ca to use to validate servers when connecting --vtctld_grpc_cert string the cert to use to connect --vtctld_grpc_crl string the server crl to use to validate server certificates when connecting diff --git a/go/flags/endtoend/vttestserver.txt b/go/flags/endtoend/vttestserver.txt index 8f47af85ba..8ee3d729ec 100644 --- a/go/flags/endtoend/vttestserver.txt +++ b/go/flags/endtoend/vttestserver.txt @@ -142,7 +142,7 @@ Usage of vttestserver: --version print binary version --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging --vschema_ddl_authorized_users string Comma separated list of users authorized to execute vschema ddl operations via vtgate - --vtctl_client_protocol string the protocol to use to talk to the vtctl server (default "grpc") + --vtctl_client_protocol string Protocol to use to talk to the vtctl server. (default "grpc") --vtctld_grpc_ca string the server ca to use to validate servers when connecting --vtctld_grpc_cert string the cert to use to connect --vtctld_grpc_crl string the server crl to use to validate server certificates when connecting diff --git a/go/vt/vtctl/vtctlclient/interface.go b/go/vt/vtctl/vtctlclient/interface.go index 9af648e759..e9bf0cdc25 100644 --- a/go/vt/vtctl/vtctlclient/interface.go +++ b/go/vt/vtctl/vtctlclient/interface.go @@ -18,18 +18,32 @@ limitations under the License. package vtctlclient import ( - "flag" + "context" "fmt" "time" - "context" + "github.com/spf13/pflag" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/logutil" + "vitess.io/vitess/go/vt/servenv" ) // vtctlClientProtocol specifics which RPC client implementation should be used. -var vtctlClientProtocol = flag.String("vtctl_client_protocol", "grpc", "the protocol to use to talk to the vtctl server") +var vtctlClientProtocol = "grpc" + +func RegisterFlags(fs *pflag.FlagSet) { + fs.StringVar(&vtctlClientProtocol, "vtctl_client_protocol", vtctlClientProtocol, "Protocol to use to talk to the vtctl server.") +} + +func init() { + for _, cmd := range []string{ + "vtctlclient", + "vttestserver", + } { + servenv.OnParseFor(cmd, RegisterFlags) + } +} // VtctlClient defines the interface used to send remote vtctl commands type VtctlClient interface { @@ -67,9 +81,9 @@ func UnregisterFactoryForTest(name string) { // New allows a user of the client library to get its implementation. func New(addr string) (VtctlClient, error) { - factory, ok := factories[*vtctlClientProtocol] + factory, ok := factories[vtctlClientProtocol] if !ok { - return nil, fmt.Errorf("unknown vtctl client protocol: %v", *vtctlClientProtocol) + return nil, fmt.Errorf("unknown vtctl client protocol: %v", vtctlClientProtocol) } return factory(addr) } diff --git a/go/vt/wrangler/testlib/vtctl_pipe.go b/go/vt/wrangler/testlib/vtctl_pipe.go index 1b39932dbe..38c535f005 100644 --- a/go/vt/wrangler/testlib/vtctl_pipe.go +++ b/go/vt/wrangler/testlib/vtctl_pipe.go @@ -18,7 +18,7 @@ package testlib import ( "bytes" - "flag" + "context" "fmt" "io" "net" @@ -26,10 +26,10 @@ import ( "testing" "time" + "github.com/spf13/pflag" + "github.com/stretchr/testify/require" "google.golang.org/grpc" - "context" - "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/servenv" "vitess.io/vitess/go/vt/topo" @@ -56,7 +56,14 @@ func NewVtctlPipe(t *testing.T, ts *topo.Server) *VtctlPipe { // Register all vtctl commands servenvInitialized.Do(func() { // make sure we use the right protocol - flag.Set("vtctl_client_protocol", "grpc") + fs := pflag.NewFlagSet("", pflag.ContinueOnError) + vtctlclient.RegisterFlags(fs) + + err := fs.Parse([]string{ + "--vtctl_client_protocol", + "grpc", + }) + require.NoError(t, err, "failed to set `--vtctl_client_protocol=%s`", "grpc") servenv.FireRunHooks() })