vitess.io/vitess@v0.16.2/go/cmd/vtctldclient/command/legacy_shim.go (about)

     1  /*
     2  Copyright 2021 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 command
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"vitess.io/vitess/go/cmd/vtctldclient/cli"
    27  	"vitess.io/vitess/go/internal/flag"
    28  	"vitess.io/vitess/go/vt/log"
    29  	"vitess.io/vitess/go/vt/logutil"
    30  	"vitess.io/vitess/go/vt/vtctl/vtctlclient"
    31  
    32  	logutilpb "vitess.io/vitess/go/vt/proto/logutil"
    33  )
    34  
    35  var (
    36  	// LegacyVtctlCommand provides a shim to make legacy ExecuteVtctlCommand
    37  	// RPCs. This allows users to use a single binary to make RPCs against both
    38  	// the new and old vtctld gRPC APIs.
    39  	LegacyVtctlCommand = &cobra.Command{
    40  		Use:                   "LegacyVtctlCommand -- <command> [flags ...] [args ...]",
    41  		Short:                 "Invoke a legacy vtctlclient command. Flag parsing is best effort.",
    42  		DisableFlagsInUseLine: true,
    43  		Args:                  cobra.ArbitraryArgs,
    44  		RunE: func(cmd *cobra.Command, args []string) error {
    45  			cli.FinishedParsing(cmd)
    46  			return runLegacyCommand(args)
    47  		},
    48  		Long: strings.TrimSpace(`
    49  LegacyVtctlCommand uses the legacy vtctl grpc client to make an ExecuteVtctlCommand
    50  rpc to a vtctld.
    51  
    52  This command exists to support a smooth transition of any scripts that relied on
    53  vtctlclient during the migration to the new vtctldclient, and will be removed,
    54  following the Vitess project's standard deprecation cycle, once all commands
    55  have been migrated to the new VtctldServer api.
    56  
    57  To see the list of available legacy commands, run "LegacyVtctlCommand -- help".
    58  Note that, as with the old client, this requires a running server, as the flag
    59  parsing and help/usage text generation, is done server-side.
    60  
    61  Also note that, in order to defer that flag parsing to the server side, you must
    62  use the double-dash ("--") after the LegacyVtctlCommand subcommand string, or
    63  the client-side flag parsing library we are using will attempt to parse those
    64  flags (and fail).
    65  `),
    66  		Example: strings.TrimSpace(`
    67  LegacyVtctlCommand help # displays this help message
    68  LegacyVtctlCommand -- help # displays help for supported legacy vtctl commands
    69  
    70  # When using legacy command that take arguments, a double dash must be used
    71  # before the first flag argument, like in the first example. The double dash may
    72  # be used, however, at any point after the "LegacyVtctlCommand" string, as in
    73  # the second example.
    74  LegacyVtctlCommand AddCellInfo -- --server_address "localhost:1234" --root "/vitess/cell1"
    75  LegacyVtctlCommand -- AddCellInfo --server_address "localhost:5678" --root "/vitess/cell1"`),
    76  	}
    77  )
    78  
    79  func runLegacyCommand(args []string) error {
    80  	// Duplicated (mostly) from go/cmd/vtctlclient/main.go.
    81  	logger := logutil.NewConsoleLogger()
    82  
    83  	ctx, cancel := context.WithTimeout(context.Background(), actionTimeout)
    84  	defer cancel()
    85  
    86  	err := vtctlclient.RunCommandAndWait(ctx, server, args, func(e *logutilpb.Event) {
    87  		logutil.LogEvent(logger, e)
    88  	})
    89  	if err != nil {
    90  		if strings.Contains(err.Error(), "flag: help requested") {
    91  			// Help is caught by SetHelpFunc, so we don't want to indicate this as an error.
    92  			return nil
    93  		}
    94  
    95  		errStr := strings.Replace(err.Error(), "remote error: ", "", -1)
    96  		fmt.Printf("%s Error: %s\n", flag.Arg(0), errStr)
    97  		log.Error(err)
    98  	}
    99  
   100  	return err
   101  }
   102  
   103  func init() {
   104  	Root.AddCommand(LegacyVtctlCommand)
   105  }