Skip to content

Commit

Permalink
cmd: replace args with specific argument in helper functions
Browse files Browse the repository at this point in the history
Namely this commit fixes:
- resolveConnectOpts
- replicasetFillCtx

Part of #1075
  • Loading branch information
elhimov committed Dec 27, 2024
1 parent 035ea0a commit 4974301
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
29 changes: 14 additions & 15 deletions cli/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -159,34 +158,34 @@ 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)
}
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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
}

Expand Down
33 changes: 17 additions & 16 deletions cli/cmd/replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 != "" {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 4974301

Please sign in to comment.