vitess.io/vitess@v0.16.2/go/cmd/vtctlclient/main.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 main
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"fmt"
    23  	"os"
    24  	"strings"
    25  	"time"
    26  
    27  	"github.com/spf13/pflag"
    28  
    29  	"vitess.io/vitess/go/acl"
    30  	"vitess.io/vitess/go/exit"
    31  	"vitess.io/vitess/go/trace"
    32  	"vitess.io/vitess/go/vt/log"
    33  	"vitess.io/vitess/go/vt/logutil"
    34  	"vitess.io/vitess/go/vt/servenv"
    35  	"vitess.io/vitess/go/vt/vtctl/vtctlclient"
    36  
    37  	logutilpb "vitess.io/vitess/go/vt/proto/logutil"
    38  	// Include deprecation warnings for soon-to-be-unsupported flag invocations.
    39  )
    40  
    41  // The default values used by these flags cannot be taken from wrangler and
    42  // actionnode modules, as we don't want to depend on them at all.
    43  var (
    44  	actionTimeout = time.Hour
    45  	server        string
    46  )
    47  
    48  func init() {
    49  	servenv.OnParse(func(fs *pflag.FlagSet) {
    50  		fs.DurationVar(&actionTimeout, "action_timeout", actionTimeout, "timeout for the total command")
    51  		fs.StringVar(&server, "server", server, "server to use for connection")
    52  
    53  		acl.RegisterFlags(fs)
    54  	})
    55  }
    56  
    57  // checkDeprecations runs quick and dirty checks to see whether any command or flag are deprecated.
    58  // For any depracated command or flag, the function issues a warning message.
    59  // this function will change on each Vitess version. Each depracation message should only last a version.
    60  // VEP-4 will replace the need for this function. See https://github.com/vitessio/enhancements/blob/main/veps/vep-4.md
    61  func checkDeprecations(args []string) {
    62  	// utility:
    63  	// name this to findSubstring if you need to use it
    64  	_ = func(s string) (arg string, ok bool) {
    65  		for _, arg := range args {
    66  			if strings.Contains(arg, s) {
    67  				return arg, true
    68  			}
    69  		}
    70  		return "", false
    71  	}
    72  }
    73  
    74  func main() {
    75  	defer exit.Recover()
    76  
    77  	args := servenv.ParseFlagsWithArgs("vtctlclient")
    78  
    79  	closer := trace.StartTracing("vtctlclient")
    80  	defer trace.LogErrorsWhenClosing(closer)
    81  
    82  	logger := logutil.NewConsoleLogger()
    83  
    84  	// We can't do much without a --server flag
    85  	if server == "" {
    86  		log.Error(errors.New("please specify --server <vtctld_host:vtctld_port> to specify the vtctld server to connect to"))
    87  		os.Exit(1)
    88  	}
    89  
    90  	ctx, cancel := context.WithTimeout(context.Background(), actionTimeout)
    91  	defer cancel()
    92  
    93  	checkDeprecations(args)
    94  
    95  	err := vtctlclient.RunCommandAndWait(ctx, server, args, func(e *logutilpb.Event) {
    96  		logutil.LogEvent(logger, e)
    97  	})
    98  	if err != nil {
    99  		if strings.Contains(err.Error(), "flag: help requested") {
   100  			return
   101  		}
   102  
   103  		errStr := strings.Replace(err.Error(), "remote error: ", "", -1)
   104  		fmt.Printf("%s Error: %s\n", args[0], errStr)
   105  		log.Error(err)
   106  		os.Exit(1)
   107  	}
   108  }