[vtctlclient] Migrate `vtctl_client_protocol` to pflag (#11368)

* [vtctlclient] Migrate `vtctl_client_protocol` to pflag

Closes #10892.

Signed-off-by: Andrew Mason <andrew@planetscale.com>

* forgot to update testdata

Signed-off-by: Andrew Mason <andrew@planetscale.com>
This commit is contained in:
Andrew Mason 2022-09-28 11:15:30 -04:00 коммит произвёл GitHub
Родитель cf4acaaae3
Коммит a984f9e624
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 35 добавлений и 12 удалений

Просмотреть файл

@ -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[:]

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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)
}

Просмотреть файл

@ -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()
})