Skip to content

Commit

Permalink
remove code duplication in Command.Run functions
Browse files Browse the repository at this point in the history
Closes #1072
  • Loading branch information
elhimov committed Dec 25, 2024
1 parent 7a08f49 commit 655c55b
Show file tree
Hide file tree
Showing 22 changed files with 137 additions and 425 deletions.
11 changes: 2 additions & 9 deletions cli/cmd/aeon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/spf13/cobra"
aeon "github.com/tarantool/tt/cli/aeon/cmd"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/modules"
"github.com/tarantool/tt/cli/util"
libconnect "github.com/tarantool/tt/lib/connect"
)
Expand All @@ -17,7 +16,7 @@ var aeonConnectCtx = aeon.ConnectCtx{
}

func newAeonConnectCmd() *cobra.Command {
var aeonCmd = &cobra.Command{
var aeonCmd = setupTtModuleCmd(internalAeonConnect, &cobra.Command{
Use: "connect URI",
Short: "Connect to the aeon instance",
Long: "Connect to the aeon instance.\n\n" +
Expand All @@ -28,13 +27,7 @@ func newAeonConnectCmd() *cobra.Command {
util.HandleCmdErr(cmd, err)
return err
},
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalAeonConnect, args)
util.HandleCmdErr(cmd, err)
},
}
})
aeonCmd.Flags().StringVar(&aeonConnectCtx.Ssl.KeyFile, "sslkeyfile", "",
"path to a private SSL key file")
aeonCmd.Flags().StringVar(&aeonConnectCtx.Ssl.CertFile, "sslcertfile", "",
Expand Down
11 changes: 2 additions & 9 deletions cli/cmd/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/spf13/cobra"
"github.com/tarantool/tt/cli/checkpoint"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/modules"
"github.com/tarantool/tt/cli/util"
"github.com/tarantool/tt/cli/version"
)
Expand All @@ -31,20 +30,14 @@ var catFlags = checkpoint.Opts{

// NewCatCmd creates a new cat command.
func NewCatCmd() *cobra.Command {
var catCmd = &cobra.Command{
var catCmd = setupTtModuleCmd(internalCatModule, &cobra.Command{
Use: "cat <FILE>...",
Short: "Print into stdout the contents of .snap/.xlog FILE(s)",
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalCatModule, args)
util.HandleCmdErr(cmd, err)
},
Example: "tt cat /path/to/file.snap /path/to/file.xlog /path/to/dir/ " +
"--timestamp 2024-11-13T14:02:36.818700000+00:00\n" +
" tt cat /path/to/file.snap /path/to/file.xlog /path/to/dir/ " +
"--timestamp=1731592956.818",
}
})

catCmd.Flags().Uint64Var(&catFlags.To, "to", catFlags.To,
"Show operations ending with the given lsn")
Expand Down
12 changes: 2 additions & 10 deletions cli/cmd/cfg_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"github.com/spf13/cobra"
"github.com/tarantool/tt/cli/cfg"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/modules"
"github.com/tarantool/tt/cli/util"
)

var (
Expand All @@ -16,16 +14,10 @@ var (

// NewDumpCmd creates a new dump command.
func NewDumpCmd() *cobra.Command {
var dumpCmd = &cobra.Command{
var dumpCmd = setupTtModuleCmd(internalDumpModule, &cobra.Command{
Use: "dump",
Short: "Print environment configuration",
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalDumpModule, args)
util.HandleCmdErr(cmd, err)
},
}
})

dumpCmd.Flags().BoolVarP(&rawDump, "raw", "r", false,
"Display the raw contents of tt environment config.")
Expand Down
12 changes: 2 additions & 10 deletions cli/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,15 @@ import (
"github.com/apex/log"
"github.com/spf13/cobra"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/modules"
"github.com/tarantool/tt/cli/running"
"github.com/tarantool/tt/cli/util"
)

// NewCheckCmd creates a new check command.
func NewCheckCmd() *cobra.Command {
var checkCmd = &cobra.Command{
var checkCmd = setupTtModuleCmd(internalCheckModule, &cobra.Command{
Use: "check [<APPLICATION_NAME>]",
Short: "Check an application file for syntax errors",
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalCheckModule, args)
util.HandleCmdErr(cmd, err)
},
}
})

return checkCmd
}
Expand Down
101 changes: 23 additions & 78 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
clustercmd "github.com/tarantool/tt/cli/cluster/cmd"
"github.com/tarantool/tt/cli/cmd/internal"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/modules"
"github.com/tarantool/tt/cli/replicaset"
"github.com/tarantool/tt/cli/running"
"github.com/tarantool/tt/cli/util"
Expand Down Expand Up @@ -141,19 +140,13 @@ func newClusterReplicasetCmd() *cobra.Command {
Aliases: []string{"rs"},
}

promoteCmd := &cobra.Command{
promoteCmd := setupTtModuleCmd(internalClusterReplicasetPromoteModule, &cobra.Command{
Use: "promote [-f] [flags] <URI> <INSTANCE_NAME>",
DisableFlagsInUseLine: true,
Short: "Promote an instance",
Long: "Promote an instance\n\n" + clusterUriHelp,
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalClusterReplicasetPromoteModule, args)
util.HandleCmdErr(cmd, err)
},
Args: cobra.ExactArgs(2),
}
Args: cobra.ExactArgs(2),
})
promoteCmd.Flags().StringVarP(&promoteCtx.Username, "username", "u", "",
"username (used as etcd/tarantool config storage credentials)")
promoteCmd.Flags().StringVarP(&promoteCtx.Password, "password", "p", "",
Expand All @@ -162,19 +155,13 @@ func newClusterReplicasetCmd() *cobra.Command {
"skip selecting a key for patching")
integrity.RegisterWithIntegrityFlag(promoteCmd.Flags(), &clusterIntegrityPrivateKey)

demoteCmd := &cobra.Command{
demoteCmd := setupTtModuleCmd(internalClusterReplicasetDemoteModule, &cobra.Command{
Use: "demote [-f] [flags] <URI> <INSTANCE_NAME>",
DisableFlagsInUseLine: true,
Short: "Demote an instance",
Long: "Demote an instance\n\n" + clusterUriHelp,
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalClusterReplicasetDemoteModule, args)
util.HandleCmdErr(cmd, err)
},
Args: cobra.ExactArgs(2),
}
Args: cobra.ExactArgs(2),
})

demoteCmd.Flags().StringVarP(&demoteCtx.Username, "username", "u", "",
"username (used as etcd/tarantool config storage credentials)")
Expand All @@ -184,19 +171,13 @@ func newClusterReplicasetCmd() *cobra.Command {
"skip selecting a key for patching")
integrity.RegisterWithIntegrityFlag(demoteCmd.Flags(), &clusterIntegrityPrivateKey)

expelCmd := &cobra.Command{
expelCmd := setupTtModuleCmd(internalClusterReplicasetExpelModule, &cobra.Command{
Use: "expel [-f] [flags] <URI> <INSTANCE_NAME>",
DisableFlagsInUseLine: true,
Short: "Expel an instance",
Long: "Expel an instance\n\n" + clusterUriHelp,
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalClusterReplicasetExpelModule, args)
util.HandleCmdErr(cmd, err)
},
Args: cobra.ExactArgs(2),
}
Args: cobra.ExactArgs(2),
})

expelCmd.Flags().StringVarP(&expelCtx.Username, "username", "u", "",
"username (used as etcd/tarantool config storage credentials)")
Expand All @@ -211,20 +192,14 @@ func newClusterReplicasetCmd() *cobra.Command {
Short: "Add or remove roles in cluster replicaset",
}

addRolesCmd := &cobra.Command{
addRolesCmd := setupTtModuleCmd(internalClusterReplicasetRolesAddModule, &cobra.Command{
Use: "add <URI> <ROLE_NAME> [flags]",
Short: "Add role to an instance, group or instance",
Long: "Add role to an instance, group or instance\n\n" + clusterUriHelp,
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalClusterReplicasetRolesAddModule, args)
util.HandleCmdErr(cmd, err)
},
Example: "tt cluster replicaset roles add http://user:pass@localhost:3301" +
" roles.metrics-export --instance_name master",
Args: cobra.ExactArgs(2),
}
})

addRolesCmd.Flags().StringVarP(&rolesChangeCtx.ReplicasetName, "replicaset", "r", "",
"name of a target replicaset")
Expand All @@ -243,20 +218,14 @@ func newClusterReplicasetCmd() *cobra.Command {
"skip selecting a key for patching")
integrity.RegisterWithIntegrityFlag(addRolesCmd.Flags(), &clusterIntegrityPrivateKey)

removeRolesCmd := &cobra.Command{
removeRolesCmd := setupTtModuleCmd(internalClusterReplicasetRolesRemoveModule, &cobra.Command{
Use: "remove <URI> <ROLE_NAME> [flags]",
Short: "Remove role from instance, group, instance or globally",
Long: "Remove role from instance, group, instance or globally\n\n" + clusterUriHelp,
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalClusterReplicasetRolesRemoveModule, args)
util.HandleCmdErr(cmd, err)
},
Example: "tt cluster replicaset roles remove http://user:pass@localhost:3301" +
" roles.metrics-export --instance_name master",
Args: cobra.ExactArgs(2),
}
})

removeRolesCmd.Flags().StringVarP(&rolesChangeCtx.ReplicasetName, "replicaset", "r", "",
"name of a target replicaset")
Expand Down Expand Up @@ -293,20 +262,14 @@ func newClusterFailoverCmd() *cobra.Command {
Aliases: []string{"fo"},
}

switchCmd := &cobra.Command{
switchCmd := setupTtModuleCmd(internalClusterFailoverSwitchModule, &cobra.Command{
Use: "switch <URI> <INSTANCE_NAME> [flags]",
DisableFlagsInUseLine: true,
Short: "Switch master instance",
Long: "Switch master instance\n\n" + failoverUriHelp,
Example: "tt cluster failover switch http://localhost:2379/app instance_name",
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalClusterFailoverSwitchModule, args)
util.HandleCmdErr(cmd, err)
},
Args: cobra.ExactArgs(2),
}
Args: cobra.ExactArgs(2),
})

switchCmd.Flags().StringVarP(&switchCtx.Username, "username", "u", "",
"username (used as etcd credentials)")
Expand All @@ -317,19 +280,13 @@ func newClusterFailoverCmd() *cobra.Command {
switchCmd.Flags().BoolVarP(&switchCtx.Wait, "wait", "w", false,
"wait for the command to complete execution")

switchStatusCmd := &cobra.Command{
switchStatusCmd := setupTtModuleCmd(internalClusterFailoverSwitchStatusModule, &cobra.Command{
Use: "switch-status <URI> <TASK_ID>",
DisableFlagsInUseLine: true,
Short: "Show master switching status",
Long: "Show master switching status\n\n" + failoverUriHelp,
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalClusterFailoverSwitchStatusModule, args)
util.HandleCmdErr(cmd, err)
},
Args: cobra.ExactArgs(2),
}
Args: cobra.ExactArgs(2),
})

cmd.AddCommand(switchCmd)
cmd.AddCommand(switchStatusCmd)
Expand All @@ -343,7 +300,7 @@ func NewClusterCmd() *cobra.Command {
Short: "Manage cluster configuration",
}

show := &cobra.Command{
show := setupTtModuleCmd(internalClusterShowModule, &cobra.Command{
Use: "show (<APP_NAME> | <APP_NAME:INSTANCE_NAME> | <URI>)",
Short: "Show a cluster configuration",
Long: "Show a cluster configuration for an application, instance," +
Expand All @@ -352,12 +309,6 @@ func NewClusterCmd() *cobra.Command {
" tt cluster show application_name:instance_name\n" +
" tt cluster show https://user:pass@localhost:2379/tt\n" +
" tt cluster show https://user:pass@localhost:2379/tt?name=instance",
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalClusterShowModule, args)
util.HandleCmdErr(cmd, err)
},
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(
cmd *cobra.Command,
Expand All @@ -371,7 +322,7 @@ func NewClusterCmd() *cobra.Command {
running.ExtractActiveAppNames,
running.ExtractActiveInstanceNames)
},
}
})
show.Flags().StringVarP(&showCtx.Username, "username", "u", "",
"username (used as etcd credentials only)")
show.Flags().StringVarP(&showCtx.Password, "password", "p", "",
Expand All @@ -380,7 +331,7 @@ func NewClusterCmd() *cobra.Command {
"validate the configuration")
clusterCmd.AddCommand(show)

publish := &cobra.Command{
publish := setupTtModuleCmd(internalClusterPublishModule, &cobra.Command{
Use: "publish (<APP_NAME> | <APP_NAME:INSTANCE_NAME> | <URI>) file",
Short: "Publish a cluster configuration",
Long: "Publish an application or an instance configuration to a cluster " +
Expand All @@ -399,12 +350,6 @@ func NewClusterCmd() *cobra.Command {
" tt cluster publish --group group --replicaset replicaset " +
"https://user:pass@localhost:2379/tt?name=instance " +
"instance.yaml",
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalClusterPublishModule, args)
util.HandleCmdErr(cmd, err)
},
Args: cobra.ExactArgs(2),
ValidArgsFunction: func(
cmd *cobra.Command,
Expand All @@ -415,7 +360,7 @@ func NewClusterCmd() *cobra.Command {
running.ExtractActiveAppNames,
running.ExtractActiveInstanceNames)
},
}
})
publish.Flags().StringVarP(&publishCtx.Username, "username", "u", "",
"username (used as etcd credentials only)")
publish.Flags().StringVarP(&publishCtx.Password, "password", "p", "",
Expand Down
14 changes: 14 additions & 0 deletions cli/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"fmt"
"io"

"github.com/spf13/cobra"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/configure"
"github.com/tarantool/tt/cli/modules"
"github.com/tarantool/tt/cli/util"
libcluster "github.com/tarantool/tt/lib/cluster"
"github.com/tarantool/tt/lib/integrity"
)
Expand Down Expand Up @@ -67,3 +70,14 @@ func createDataCollectorsAndDataPublishers(ctx integrity.IntegrityCtx,
}
return collectors, publishers, err
}

func setupTtModuleCmd(internalModule modules.InternalFunc, cmd *cobra.Command) *cobra.Command {
cmd.Run = func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalModule, args)
if err != nil {
util.HandleCmdErr(cmd, err)
}
}
return cmd
}
Loading

0 comments on commit 655c55b

Please sign in to comment.