From 4974301250c6eb1482a34554459809c5dec41bcd Mon Sep 17 00:00:00 2001 From: Mikhail Elhimov Date: Fri, 27 Dec 2024 00:02:19 +0300 Subject: [PATCH] cmd: replace `args` with specific argument in helper functions Namely this commit fixes: - resolveConnectOpts - replicasetFillCtx Part of #1075 --- cli/cmd/connect.go | 29 ++++++++++++++--------------- cli/cmd/replicaset.go | 33 +++++++++++++++++---------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/cli/cmd/connect.go b/cli/cmd/connect.go index b5d25124e..65e6b819a 100644 --- a/cli/cmd/connect.go +++ b/cli/cmd/connect.go @@ -129,18 +129,17 @@ func makeConnOpts(network, address string, connCtx connect.ConnectCtx) connector } } -// resolveConnectOpts tries to resolve the first passed argument as an instance -// name to replace it with a control socket or as a URI with/without -// credentials. +// resolveConnectOpts tries to resolve target argument to replace it +// with a control socket or as a URI with/without credentials. // It returns connection options and the remaining args. func resolveConnectOpts(cmdCtx *cmdcontext.CmdCtx, cliOpts *config.CliOpts, - connectCtx *connect.ConnectCtx, args []string) ( - connOpts connector.ConnectOpts, newArgs []string, err error) { + connectCtx *connect.ConnectCtx, target string) ( + connOpts connector.ConnectOpts, err error) { - newArgs = args[1:] // FillCtx returns error if no instances found. var runningCtx running.RunningCtx - if fillErr := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); fillErr == nil { + fillErr := running.FillCtx(cliOpts, cmdCtx, &runningCtx, []string{target}, false) + if fillErr == nil { if len(runningCtx.Instances) > 1 { err = fmt.Errorf("specify instance name") return @@ -159,19 +158,19 @@ func resolveConnectOpts(cmdCtx *cmdcontext.CmdCtx, cliOpts *config.CliOpts, connector.UnixNetwork, runningCtx.Instances[0].ConsoleSocket, *connectCtx, ) } - } else if libconnect.IsCredentialsURI(args[0]) { + } else if libconnect.IsCredentialsURI(target) { if connectCtx.Username != "" || connectCtx.Password != "" { err = fmt.Errorf("username and password are specified with" + " flags and a URI") return } - newURI, user, pass := libconnect.ParseCredentialsURI(args[0]) + newURI, user, pass := libconnect.ParseCredentialsURI(target) network, address := libconnect.ParseBaseURI(newURI) connectCtx.Username = user connectCtx.Password = pass connOpts = makeConnOpts(network, address, *connectCtx) connectCtx.ConnectTarget = newURI - } else if libconnect.IsBaseURI(args[0]) { + } else if libconnect.IsBaseURI(target) { // Environment variables do not overwrite values. if connectCtx.Username == "" { connectCtx.Username = os.Getenv(libconnect.TarantoolUsernameEnv) @@ -179,14 +178,14 @@ func resolveConnectOpts(cmdCtx *cmdcontext.CmdCtx, cliOpts *config.CliOpts, if connectCtx.Password == "" { connectCtx.Password = os.Getenv(libconnect.TarantoolPasswordEnv) } - network, address := libconnect.ParseBaseURI(args[0]) + network, address := libconnect.ParseBaseURI(target) connOpts = makeConnOpts(network, address, *connectCtx) } else { err = fillErr return } if connectCtx.ConnectTarget == "" { - connectCtx.ConnectTarget = args[0] + connectCtx.ConnectTarget = target } return } @@ -214,13 +213,13 @@ func internalConnectModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { return util.NewArgError(fmt.Sprintf("unsupported output format: %s", connectFormat)) } - connOpts, newArgs, err := resolveConnectOpts(cmdCtx, cliOpts, &connectCtx, args) + connOpts, err := resolveConnectOpts(cmdCtx, cliOpts, &connectCtx, args[0]) if err != nil { return err } if connectFile != "" { - res, err := connect.Eval(connectCtx, connOpts, newArgs) + res, err := connect.Eval(connectCtx, connOpts, args[1:]) if err != nil { return err } @@ -230,7 +229,7 @@ func internalConnectModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { if !connectInteractive || !terminal.IsTerminal(syscall.Stdin) { return nil } - } else if len(newArgs) != 0 { + } else if len(args) != 1 { return fmt.Errorf("should be specified one connection string") } diff --git a/cli/cmd/replicaset.go b/cli/cmd/replicaset.go index 42ecaa971..f6939bfa2 100644 --- a/cli/cmd/replicaset.go +++ b/cli/cmd/replicaset.go @@ -479,7 +479,7 @@ type replicasetCtx struct { } // replicasetFillCtx fills the replicaset command context. -func replicasetFillCtx(cmdCtx *cmdcontext.CmdCtx, ctx *replicasetCtx, args []string, +func replicasetFillCtx(cmdCtx *cmdcontext.CmdCtx, ctx *replicasetCtx, target string, isRunningCtxRequired bool) error { var err error ctx.Orchestrator, err = getOrchestrator() @@ -497,7 +497,8 @@ func replicasetFillCtx(cmdCtx *cmdcontext.CmdCtx, ctx *replicasetCtx, args []str } var connOpts connector.ConnectOpts - if err := running.FillCtx(cliOpts, cmdCtx, &ctx.RunningCtx, args, false); err == nil { + err = running.FillCtx(cliOpts, cmdCtx, &ctx.RunningCtx, []string{target}, false) + if err == nil { ctx.IsApplication = true if len(ctx.RunningCtx.Instances) == 1 { if connectCtx.Username != "" || connectCtx.Password != "" { @@ -511,7 +512,7 @@ func replicasetFillCtx(cmdCtx *cmdcontext.CmdCtx, ctx *replicasetCtx, args []str connectCtx, ) ctx.IsInstanceConnect = true - appName, instName, found := strings.Cut(args[0], string(running.InstanceDelimiter)) + appName, instName, found := strings.Cut(target, string(running.InstanceDelimiter)) if found { if instName != ctx.RunningCtx.Instances[0].InstName { return fmt.Errorf("instance %q not found", instName) @@ -554,7 +555,7 @@ func replicasetFillCtx(cmdCtx *cmdcontext.CmdCtx, ctx *replicasetCtx, args []str if isRunningCtxRequired { return err } - connOpts, _, err = resolveConnectOpts(cmdCtx, cliOpts, &connectCtx, args) + connOpts, err = resolveConnectOpts(cmdCtx, cliOpts, &connectCtx, target) if err != nil { return err } @@ -576,7 +577,7 @@ func replicasetFillCtx(cmdCtx *cmdcontext.CmdCtx, ctx *replicasetCtx, args []str // internalReplicasetUpgradeModule is a "upgrade" command for the replicaset module. func internalReplicasetUpgradeModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { var ctx replicasetCtx - if err := replicasetFillCtx(cmdCtx, &ctx, args, false); err != nil { + if err := replicasetFillCtx(cmdCtx, &ctx, args[0], false); err != nil { return err } if ctx.IsInstanceConnect { @@ -592,7 +593,7 @@ func internalReplicasetUpgradeModule(cmdCtx *cmdcontext.CmdCtx, args []string) e SslCiphers: replicasetSslCiphers, } var connOpts connector.ConnectOpts - connOpts, _, _ = resolveConnectOpts(cmdCtx, cliOpts, &connectCtx, args) + connOpts, _ = resolveConnectOpts(cmdCtx, cliOpts, &connectCtx, args[0]) return replicasetcmd.Upgrade(replicasetcmd.DiscoveryCtx{ IsApplication: ctx.IsApplication, @@ -608,7 +609,7 @@ func internalReplicasetUpgradeModule(cmdCtx *cmdcontext.CmdCtx, args []string) e // internalReplicasetDowngradeModule is a "upgrade" command for the replicaset module. func internalReplicasetDowngradeModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { var ctx replicasetCtx - if err := replicasetFillCtx(cmdCtx, &ctx, args, false); err != nil { + if err := replicasetFillCtx(cmdCtx, &ctx, args[0], false); err != nil { return err } if ctx.IsInstanceConnect { @@ -624,7 +625,7 @@ func internalReplicasetDowngradeModule(cmdCtx *cmdcontext.CmdCtx, args []string) SslCiphers: replicasetSslCiphers, } var connOpts connector.ConnectOpts - connOpts, _, _ = resolveConnectOpts(cmdCtx, cliOpts, &connectCtx, args) + connOpts, _ = resolveConnectOpts(cmdCtx, cliOpts, &connectCtx, args[0]) return replicasetcmd.Downgrade(replicasetcmd.DiscoveryCtx{ IsApplication: ctx.IsApplication, @@ -641,7 +642,7 @@ func internalReplicasetDowngradeModule(cmdCtx *cmdcontext.CmdCtx, args []string) // internalReplicasetPromoteModule is a "promote" command for the replicaset module. func internalReplicasetPromoteModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { var ctx replicasetCtx - if err := replicasetFillCtx(cmdCtx, &ctx, args, false); err != nil { + if err := replicasetFillCtx(cmdCtx, &ctx, args[0], false); err != nil { return err } if !ctx.IsInstanceConnect { @@ -671,7 +672,7 @@ func internalReplicasetPromoteModule(cmdCtx *cmdcontext.CmdCtx, args []string) e // internalReplicasetDemoteModule is a "demote" command for the replicaset module. func internalReplicasetDemoteModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { var ctx replicasetCtx - if err := replicasetFillCtx(cmdCtx, &ctx, args, true); err != nil { + if err := replicasetFillCtx(cmdCtx, &ctx, args[0], true); err != nil { return err } if !ctx.IsApplication { @@ -703,7 +704,7 @@ func internalReplicasetDemoteModule(cmdCtx *cmdcontext.CmdCtx, args []string) er // internalReplicasetStatusModule is a "status" command for the replicaset module. func internalReplicasetStatusModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { var ctx replicasetCtx - if err := replicasetFillCtx(cmdCtx, &ctx, args, false); err != nil { + if err := replicasetFillCtx(cmdCtx, &ctx, args[0], false); err != nil { return err } if ctx.IsInstanceConnect { @@ -723,7 +724,7 @@ func internalReplicasetExpelModule(cmdCtx *cmdcontext.CmdCtx, args []string) err return fmt.Errorf("the command expects argument application_name:instance_name") } var ctx replicasetCtx - if err := replicasetFillCtx(cmdCtx, &ctx, args, true); err != nil { + if err := replicasetFillCtx(cmdCtx, &ctx, args[0], true); err != nil { return err } if ctx.IsInstanceConnect { @@ -750,7 +751,7 @@ func internalReplicasetExpelModule(cmdCtx *cmdcontext.CmdCtx, args []string) err // the "replicaset vshard" module. func internalReplicasetBootstrapVShardModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { var ctx replicasetCtx - if err := replicasetFillCtx(cmdCtx, &ctx, args, false); err != nil { + if err := replicasetFillCtx(cmdCtx, &ctx, args[0], false); err != nil { return err } if ctx.IsInstanceConnect { @@ -777,7 +778,7 @@ func internalReplicasetBootstrapModule(cmdCtx *cmdcontext.CmdCtx, args []string) _, instName, found := strings.Cut(args[0], string(running.InstanceDelimiter)) var ctx replicasetCtx - if err := replicasetFillCtx(cmdCtx, &ctx, args, true); err != nil { + if err := replicasetFillCtx(cmdCtx, &ctx, args[0], true); err != nil { return err } if ctx.IsInstanceConnect { @@ -839,7 +840,7 @@ func internalReplicasetRebootstrapModule(cmdCtx *cmdcontext.CmdCtx, args []strin // internalReplicasetRolesAddModule is a "roles add" command for the replicaset module. func internalReplicasetRolesAddModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { var ctx replicasetCtx - if err := replicasetFillCtx(cmdCtx, &ctx, args, false); err != nil { + if err := replicasetFillCtx(cmdCtx, &ctx, args[0], false); err != nil { return err } defer ctx.Conn.Close() @@ -882,7 +883,7 @@ func internalReplicasetRolesAddModule(cmdCtx *cmdcontext.CmdCtx, args []string) // internalReplicasetRolesRemoveModule is a "roles remove" command for the replicaset module. func internalReplicasetRolesRemoveModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { var ctx replicasetCtx - if err := replicasetFillCtx(cmdCtx, &ctx, args, false); err != nil { + if err := replicasetFillCtx(cmdCtx, &ctx, args[0], false); err != nil { return err } defer ctx.Conn.Close()