diff --git a/go/cmd/vtcombo/cli/main.go b/go/cmd/vtcombo/cli/main.go index 5f27e581a24..52e29044268 100644 --- a/go/cmd/vtcombo/cli/main.go +++ b/go/cmd/vtcombo/cli/main.go @@ -126,15 +126,6 @@ func init() { // user know about this flag. Main.Flags().MarkHidden("tablet_protocol") - var err error - env, err = vtenv.New(vtenv.Options{ - MySQLServerVersion: servenv.MySQLServerVersion(), - TruncateUILen: servenv.TruncateUILen, - TruncateErrLen: servenv.TruncateErrLen, - }) - if err != nil { - log.Fatalf("unable to initialize env: %v", err) - } srvTopoCounts = stats.NewCountersWithSingleLabel("ResilientSrvTopoServer", "Resilient srvtopo server operations", "type") } @@ -189,6 +180,15 @@ func run(cmd *cobra.Command, args []string) (err error) { cmd.Flags().Set("log_dir", "$VTDATAROOT/tmp") } + env, err = vtenv.New(vtenv.Options{ + MySQLServerVersion: servenv.MySQLServerVersion(), + TruncateUILen: servenv.TruncateUILen, + TruncateErrLen: servenv.TruncateErrLen, + }) + if err != nil { + log.Fatalf("unable to initialize env: %v", err) + } + ctx, cancel := context.WithCancel(cmd.Context()) defer cancel() if externalTopoServer { diff --git a/go/cmd/vtctldclient/command/root.go b/go/cmd/vtctldclient/command/root.go index 2a65a59f606..4b16ab71462 100644 --- a/go/cmd/vtctldclient/command/root.go +++ b/go/cmd/vtctldclient/command/root.go @@ -29,7 +29,6 @@ import ( "github.com/spf13/cobra" "vitess.io/vitess/go/trace" - "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/servenv" "vitess.io/vitess/go/vt/topo" @@ -105,6 +104,15 @@ connect directly to the topo server(s).`, useInternalVtctld), // We use PersistentPreRun to set up the tracer, grpc client, and // command context for every command. PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) { + env, err = vtenv.New(vtenv.Options{ + MySQLServerVersion: servenv.MySQLServerVersion(), + TruncateUILen: servenv.TruncateUILen, + TruncateErrLen: servenv.TruncateErrLen, + }) + if err != nil { + return fmt.Errorf("failed to initialize vtenv: %w", err) + } + logutil.PurgeLogs() traceCloser = trace.StartTracing("vtctldclient") client, err = getClientForCommand(cmd) @@ -118,6 +126,7 @@ connect directly to the topo server(s).`, useInternalVtctld), } vreplcommon.SetClient(client) vreplcommon.SetCommandCtx(commandCtx) + return err }, // Similarly, PersistentPostRun cleans up the resources spawned by @@ -220,14 +229,4 @@ func init() { Root.PersistentFlags().StringSliceVar(&topoOptions.globalServerAddresses, "topo-global-server-address", topoOptions.globalServerAddresses, "the address of the global topology server(s)") Root.PersistentFlags().StringVar(&topoOptions.globalRoot, "topo-global-root", topoOptions.globalRoot, "the path of the global topology data in the global topology server") vreplcommon.RegisterCommands(Root) - - var err error - env, err = vtenv.New(vtenv.Options{ - MySQLServerVersion: servenv.MySQLServerVersion(), - TruncateUILen: servenv.TruncateUILen, - TruncateErrLen: servenv.TruncateErrLen, - }) - if err != nil { - log.Fatalf("failed to initialize vtenv: %v", err) - } }