From 655c55b80a153765d2944052fcbe27f3bdbc77e2 Mon Sep 17 00:00:00 2001 From: Mikhail Elhimov Date: Tue, 24 Dec 2024 23:23:15 +0300 Subject: [PATCH] remove code duplication in Command.Run functions Closes #1072 --- cli/cmd/aeon.go | 11 +--- cli/cmd/cat.go | 11 +--- cli/cmd/cfg_dump.go | 12 +---- cli/cmd/check.go | 12 +---- cli/cmd/cluster.go | 101 ++++++++-------------------------- cli/cmd/common.go | 14 +++++ cli/cmd/connect.go | 11 +--- cli/cmd/coredump.go | 46 +++++++--------- cli/cmd/download.go | 12 +---- cli/cmd/install.go | 42 +++------------ cli/cmd/instances.go | 14 ++--- cli/cmd/kill.go | 11 +--- cli/cmd/log.go | 12 +---- cli/cmd/logrotate.go | 12 +---- cli/cmd/play.go | 11 +--- cli/cmd/replicaset.go | 123 ++++++++++-------------------------------- cli/cmd/restart.go | 13 ++--- cli/cmd/rocks.go | 12 +---- cli/cmd/start.go | 12 +---- cli/cmd/status.go | 12 +---- cli/cmd/stop.go | 16 ++---- cli/cmd/uninstall.go | 42 +++------------ 22 files changed, 137 insertions(+), 425 deletions(-) diff --git a/cli/cmd/aeon.go b/cli/cmd/aeon.go index 0e41f0d4d..66a06d4da 100644 --- a/cli/cmd/aeon.go +++ b/cli/cmd/aeon.go @@ -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" ) @@ -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" + @@ -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", "", diff --git a/cli/cmd/cat.go b/cli/cmd/cat.go index aa0f8c8c1..3d146b947 100644 --- a/cli/cmd/cat.go +++ b/cli/cmd/cat.go @@ -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" ) @@ -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 ...", 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") diff --git a/cli/cmd/cfg_dump.go b/cli/cmd/cfg_dump.go index 10cf56eb0..575d6ce03 100644 --- a/cli/cmd/cfg_dump.go +++ b/cli/cmd/cfg_dump.go @@ -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 ( @@ -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.") diff --git a/cli/cmd/check.go b/cli/cmd/check.go index b5aed35b5..5c925ad27 100644 --- a/cli/cmd/check.go +++ b/cli/cmd/check.go @@ -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 []", 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 } diff --git a/cli/cmd/cluster.go b/cli/cmd/cluster.go index d317c655b..aa288dff3 100644 --- a/cli/cmd/cluster.go +++ b/cli/cmd/cluster.go @@ -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" @@ -141,19 +140,13 @@ func newClusterReplicasetCmd() *cobra.Command { Aliases: []string{"rs"}, } - promoteCmd := &cobra.Command{ + promoteCmd := setupTtModuleCmd(internalClusterReplicasetPromoteModule, &cobra.Command{ Use: "promote [-f] [flags] ", 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", "", @@ -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] ", 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)") @@ -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] ", 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)") @@ -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 [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") @@ -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 [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") @@ -293,20 +262,14 @@ func newClusterFailoverCmd() *cobra.Command { Aliases: []string{"fo"}, } - switchCmd := &cobra.Command{ + switchCmd := setupTtModuleCmd(internalClusterFailoverSwitchModule, &cobra.Command{ Use: "switch [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)") @@ -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 ", 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) @@ -343,7 +300,7 @@ func NewClusterCmd() *cobra.Command { Short: "Manage cluster configuration", } - show := &cobra.Command{ + show := setupTtModuleCmd(internalClusterShowModule, &cobra.Command{ Use: "show ( | | )", Short: "Show a cluster configuration", Long: "Show a cluster configuration for an application, instance," + @@ -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, @@ -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", "", @@ -380,7 +331,7 @@ func NewClusterCmd() *cobra.Command { "validate the configuration") clusterCmd.AddCommand(show) - publish := &cobra.Command{ + publish := setupTtModuleCmd(internalClusterPublishModule, &cobra.Command{ Use: "publish ( | | ) file", Short: "Publish a cluster configuration", Long: "Publish an application or an instance configuration to a cluster " + @@ -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, @@ -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", "", diff --git a/cli/cmd/common.go b/cli/cmd/common.go index d851991a3..7c52dcbb4 100644 --- a/cli/cmd/common.go +++ b/cli/cmd/common.go @@ -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" ) @@ -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 +} diff --git a/cli/cmd/connect.go b/cli/cmd/connect.go index b5d25124e..ea8d75f2b 100644 --- a/cli/cmd/connect.go +++ b/cli/cmd/connect.go @@ -13,7 +13,6 @@ import ( "github.com/tarantool/tt/cli/connect" "github.com/tarantool/tt/cli/connector" "github.com/tarantool/tt/cli/formatter" - "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/running" "github.com/tarantool/tt/cli/util" libconnect "github.com/tarantool/tt/lib/connect" @@ -37,7 +36,7 @@ var ( // NewConnectCmd creates connect command. func NewConnectCmd() *cobra.Command { - var connectCmd = &cobra.Command{ + var connectCmd = setupTtModuleCmd(internalConnectModule, &cobra.Command{ Use: "connect ( | | )" + " [flags] [-f ] [-- ARGS]\n" + " COMMAND | tt connect ( | | )" + @@ -61,12 +60,6 @@ func NewConnectCmd() *cobra.Command { "You could pass command line arguments to the interpreted SCRIPT" + " or COMMAND passed via -f flag:\n\n" + `echo "print(...)" | tt connect user:pass@localhost:3013 -f- 1, 2, 3`, - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalConnectModule, args) - util.HandleCmdErr(cmd, err) - }, Args: cobra.MinimumNArgs(1), ValidArgsFunction: func( cmd *cobra.Command, @@ -81,7 +74,7 @@ func NewConnectCmd() *cobra.Command { running.ExtractActiveInstanceNames) return validArgs, cobra.ShellCompDirectiveDefault }, - } + }) connectCmd.Flags().StringVarP(&connectUser, "username", "u", "", "username") connectCmd.Flags().StringVarP(&connectPassword, "password", "p", "", "password") diff --git a/cli/cmd/coredump.go b/cli/cmd/coredump.go index bc578568e..a21427ee7 100644 --- a/cli/cmd/coredump.go +++ b/cli/cmd/coredump.go @@ -4,8 +4,6 @@ import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/coredump" - "github.com/tarantool/tt/cli/modules" - "github.com/tarantool/tt/cli/util" ) var ( @@ -23,17 +21,11 @@ func NewCoredumpCmd() *cobra.Command { Short: "Perform manipulations with the tarantool coredumps", } - var packCmd = &cobra.Command{ + var packCmd = setupTtModuleCmd(internalCoredumpPackModule, &cobra.Command{ Use: "pack COREDUMP", Short: "pack tarantool coredump into tar.gz archive", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalCoredumpPackModule, args) - util.HandleCmdErr(cmd, err) - }, - Args: cobra.ExactArgs(1), - } + Args: cobra.ExactArgs(1), + }) packCmd.Flags().StringVarP(&coredumpPackExecutable, "executable", "e", "", "Tarantool executable path") packCmd.Flags().StringVarP(&coredumpPackOutputDirectory, "directory", "d", "", @@ -45,27 +37,17 @@ func NewCoredumpCmd() *cobra.Command { "the given process resides (see %p in core(5) for more info). This flag\n"+ "is to be used when tt is used as kernel.core_pattern pipeline script") - var unpackCmd = &cobra.Command{ + var unpackCmd = setupTtModuleCmd(internalCoredumpUnpackModule, &cobra.Command{ Use: "unpack ARCHIVE", Short: "unpack tarantool coredump tar.gz archive", - Run: func(cmd *cobra.Command, args []string) { - if err := coredump.Unpack(args[0]); err != nil { - util.HandleCmdErr(cmd, err) - } - }, - Args: cobra.ExactArgs(1), - } + Args: cobra.ExactArgs(1), + }) - var inspectCmd = &cobra.Command{ + var inspectCmd = setupTtModuleCmd(internalCoredumpInspectModule, &cobra.Command{ Use: "inspect {ARCHIVE|DIRECTORY}", Short: "inspect tarantool coredump", - Run: func(cmd *cobra.Command, args []string) { - if err := coredump.Inspect(args[0], coredumpInspectSourceDir); err != nil { - util.HandleCmdErr(cmd, err) - } - }, - Args: cobra.ExactArgs(1), - } + Args: cobra.ExactArgs(1), + }) inspectCmd.Flags().StringVarP(&coredumpInspectSourceDir, "sourcedir", "s", "", "Source directory") @@ -91,3 +73,13 @@ func internalCoredumpPackModule(cmdCtx *cmdcontext.CmdCtx, args []string) error coredumpPackTime, ) } + +// internalCoredumpUnpackModule is a default "unpack" command for the coredump module. +func internalCoredumpUnpackModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { + return coredump.Unpack(args[0]) +} + +// internalCoredumpInspectModule is a default "inspect" command for the coredump module. +func internalCoredumpInspectModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { + return coredump.Inspect(args[0], coredumpInspectSourceDir) +} diff --git a/cli/cmd/download.go b/cli/cmd/download.go index 4272aab3f..47e66d708 100644 --- a/cli/cmd/download.go +++ b/cli/cmd/download.go @@ -4,8 +4,6 @@ import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/download" - "github.com/tarantool/tt/cli/modules" - "github.com/tarantool/tt/cli/util" ) var ( @@ -14,7 +12,7 @@ var ( // newDownloadCmd Downloads and saves the Tarantool SDK. func NewDownloadCmd() *cobra.Command { - var cmd = &cobra.Command{ + var cmd = setupTtModuleCmd(internalDownloadModule, &cobra.Command{ Use: "download ", Short: `Download Tarantool SDK`, Example: ` @@ -22,13 +20,7 @@ func NewDownloadCmd() *cobra.Command { $ tt download gc64-3.0.0-0-gf58f7d82a-r23 # Download Tarantool SDK development build to the /tmp directory. $ tt download gc64-3.0.0-beta1-2-gcbb569b4c-r612 --dev --directory-prefix /tmp`, - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalDownloadModule, args) - util.HandleCmdErr(cmd, err) - }, - } + }) cmd.Flags().BoolVar(&downloadCtx.DevBuild, "dev", false, "download development build") cmd.Flags().StringVar(&downloadCtx.DirectoryPrefix, diff --git a/cli/cmd/install.go b/cli/cmd/install.go index e7aaa8917..b64c6fb78 100644 --- a/cli/cmd/install.go +++ b/cli/cmd/install.go @@ -4,40 +4,26 @@ import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/install" - "github.com/tarantool/tt/cli/modules" - "github.com/tarantool/tt/cli/util" ) var installCtx install.InstallCtx // newInstallTtCmd creates a command to install tt. func newInstallTtCmd() *cobra.Command { - var tntCmd = &cobra.Command{ + var tntCmd = setupTtModuleCmd(internalInstallModule, &cobra.Command{ Use: "tt [version|commit hash|pull-request]", Short: "Install tt", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalInstallModule, args) - util.HandleCmdErr(cmd, err) - }, - } + }) return tntCmd } // newInstallTarantoolCmd creates a command to install tarantool. func newInstallTarantoolCmd() *cobra.Command { - var tntCmd = &cobra.Command{ + var tntCmd = setupTtModuleCmd(internalInstallModule, &cobra.Command{ Use: "tarantool [version|commit hash|pull-request]", Short: "Install tarantool community edition", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalInstallModule, args) - util.HandleCmdErr(cmd, err) - }, - } + }) tntCmd.Flags().BoolVarP(&installCtx.BuildInDocker, "use-docker", "", false, "build tarantool in Ubuntu 18.04 docker container") @@ -49,16 +35,10 @@ func newInstallTarantoolCmd() *cobra.Command { // newInstallTarantoolEeCmd creates a command to install tarantool-ee. func newInstallTarantoolEeCmd() *cobra.Command { - var tntCmd = &cobra.Command{ + var tntCmd = setupTtModuleCmd(internalInstallModule, &cobra.Command{ Use: "tarantool-ee [version]", Short: "Install tarantool enterprise edition", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalInstallModule, args) - util.HandleCmdErr(cmd, err) - }, - } + }) tntCmd.Flags().BoolVar(&installCtx.DevBuild, "dev", false, "install development build") @@ -68,7 +48,7 @@ func newInstallTarantoolEeCmd() *cobra.Command { // newInstallTarantoolDevCmd creates a command to install tarantool // from the local build directory. func newInstallTarantoolDevCmd() *cobra.Command { - tntCmd := &cobra.Command{ + tntCmd := setupTtModuleCmd(internalInstallModule, &cobra.Command{ Use: "tarantool-dev ", Short: "Install tarantool from the local build directory", Example: "Assume, tarantool build directory is ~/src/tarantool/build\n" + @@ -76,13 +56,7 @@ func newInstallTarantoolDevCmd() *cobra.Command { " make -j16 -C ~/src/tarantool/build\n" + " tt install tarantool-dev ~/src/tarantool/build\n" + " tt run # runs the binary compiled above", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalInstallModule, args) - util.HandleCmdErr(cmd, err) - }, - } + }) tntCmd.Flags().StringVar(&installCtx.IncDir, "include-dir", "", "tarantool headers directory") diff --git a/cli/cmd/instances.go b/cli/cmd/instances.go index 272f54502..a237ff50d 100644 --- a/cli/cmd/instances.go +++ b/cli/cmd/instances.go @@ -4,23 +4,15 @@ import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/instances" - "github.com/tarantool/tt/cli/modules" - "github.com/tarantool/tt/cli/util" ) // NewInstancesCmd creates instances command. func NewInstancesCmd() *cobra.Command { - var instancesCmd = &cobra.Command{ + var instancesCmd = setupTtModuleCmd(internalInstancesModule, &cobra.Command{ Use: "instances", Short: "Show list of enabled applications", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalInstancesModule, args) - util.HandleCmdErr(cmd, err) - }, - Args: cobra.ExactArgs(0), - } + Args: cobra.ExactArgs(0), + }) return instancesCmd } diff --git a/cli/cmd/kill.go b/cli/cmd/kill.go index fbee974c3..cf07b82f0 100644 --- a/cli/cmd/kill.go +++ b/cli/cmd/kill.go @@ -9,7 +9,6 @@ import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmd/internal" "github.com/tarantool/tt/cli/cmdcontext" - "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/running" "github.com/tarantool/tt/cli/util" ) @@ -19,15 +18,9 @@ var dumpQuit bool // NewKillCmd creates kill command. func NewKillCmd() *cobra.Command { - var killCmd = &cobra.Command{ + var killCmd = setupTtModuleCmd(internalKillModule, &cobra.Command{ Use: "kill [ | ]", Short: "Kill tarantool instance(s)", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalKillModule, args) - util.HandleCmdErr(cmd, err) - }, ValidArgsFunction: func( cmd *cobra.Command, args []string, @@ -37,7 +30,7 @@ func NewKillCmd() *cobra.Command { running.ExtractActiveAppNames, running.ExtractActiveInstanceNames) }, - } + }) killCmd.Flags().BoolVarP(&forceKill, "force", "f", false, "do not ask for confirmation") killCmd.Flags().BoolVarP(&dumpQuit, "dump", "d", false, "quit with dump") diff --git a/cli/cmd/log.go b/cli/cmd/log.go index bbff37ccf..1f61f01c3 100644 --- a/cli/cmd/log.go +++ b/cli/cmd/log.go @@ -11,10 +11,8 @@ import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmd/internal" "github.com/tarantool/tt/cli/cmdcontext" - "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/running" "github.com/tarantool/tt/cli/tail" - "github.com/tarantool/tt/cli/util" ) var logOpts struct { @@ -24,15 +22,9 @@ var logOpts struct { // NewLogCmd creates log command. func NewLogCmd() *cobra.Command { - var logCmd = &cobra.Command{ + var logCmd = setupTtModuleCmd(internalLogModule, &cobra.Command{ Use: "log [ | ] [flags]", Short: `Get logs of instance(s)`, - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalLogModule, args) - util.HandleCmdErr(cmd, err) - }, ValidArgsFunction: func( cmd *cobra.Command, args []string, @@ -42,7 +34,7 @@ func NewLogCmd() *cobra.Command { running.ExtractAppNames, running.ExtractInstanceNames) }, - } + }) logCmd.Flags().IntVarP(&logOpts.nLines, "lines", "n", 10, "Count of last lines to output") diff --git a/cli/cmd/logrotate.go b/cli/cmd/logrotate.go index 00b70f484..6765c0ce9 100644 --- a/cli/cmd/logrotate.go +++ b/cli/cmd/logrotate.go @@ -5,22 +5,14 @@ import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmd/internal" "github.com/tarantool/tt/cli/cmdcontext" - "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/running" - "github.com/tarantool/tt/cli/util" ) // NewLogrotateCmd creates logrotate command. func NewLogrotateCmd() *cobra.Command { - var logrotateCmd = &cobra.Command{ + var logrotateCmd = setupTtModuleCmd(internalLogrotateModule, &cobra.Command{ Use: "logrotate [ | ]", Short: "Rotate logs of a started tarantool instance(s)", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalLogrotateModule, args) - util.HandleCmdErr(cmd, err) - }, ValidArgsFunction: func( cmd *cobra.Command, args []string, @@ -30,7 +22,7 @@ func NewLogrotateCmd() *cobra.Command { running.ExtractAppNames, running.ExtractInstanceNames) }, - } + }) return logrotateCmd } diff --git a/cli/cmd/play.go b/cli/cmd/play.go index 3fdf7e082..d4f8d4083 100644 --- a/cli/cmd/play.go +++ b/cli/cmd/play.go @@ -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/running" "github.com/tarantool/tt/cli/util" "github.com/tarantool/tt/cli/version" @@ -39,20 +38,14 @@ var ( // NewPlayCmd creates a new play command. func NewPlayCmd() *cobra.Command { - var playCmd = &cobra.Command{ + var playCmd = setupTtModuleCmd(internalPlayModule, &cobra.Command{ Use: "play ( | | ) ...", Short: "Play the contents of .snap/.xlog FILE(s) to another Tarantool instance", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalPlayModule, args) - util.HandleCmdErr(cmd, err) - }, Example: "tt play localhost:3013 /path/to/file.snap /path/to/file.xlog " + "/path/to/dir/ --timestamp 2024-11-13T14:02:36.818700000+00:00\n" + " tt play app:instance001 /path/to/file.snap /path/to/file.xlog " + "/path/to/dir/ --timestamp=1731592956.818", - } + }) playCmd.Flags().StringVarP(&playUsername, "username", "u", "", "username") playCmd.Flags().StringVarP(&playPassword, "password", "p", "", "password") diff --git a/cli/cmd/replicaset.go b/cli/cmd/replicaset.go index 860f60d7f..8fff67244 100644 --- a/cli/cmd/replicaset.go +++ b/cli/cmd/replicaset.go @@ -11,7 +11,6 @@ import ( "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/connect" "github.com/tarantool/tt/cli/connector" - "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/replicaset" replicasetcmd "github.com/tarantool/tt/cli/replicaset/cmd" "github.com/tarantool/tt/cli/running" @@ -60,21 +59,15 @@ var ( // newUpgradeCmd creates a "replicaset upgrade" command. func newUpgradeCmd() *cobra.Command { - cmd := &cobra.Command{ + cmd := setupTtModuleCmd(internalReplicasetUpgradeModule, &cobra.Command{ Use: "upgrade ( | ) [flags]\n\n" + replicasetUriHelp, DisableFlagsInUseLine: true, Short: "Upgrade tarantool cluster", Long: "Upgrade tarantool cluster.\n\n" + libconnect.EnvCredentialsHelp + "\n\n", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalReplicasetUpgradeModule, args) - util.HandleCmdErr(cmd, err) - }, Args: cobra.ExactArgs(1), - } + }) cmd.Flags().StringArrayVarP(&chosenReplicasetAliases, "replicaset", "r", []string{}, "specify the replicaset name(s) to upgrade") @@ -89,21 +82,15 @@ func newUpgradeCmd() *cobra.Command { // newDowngradeCmd creates a "replicaset downgrade" command. func newDowngradeCmd() *cobra.Command { - cmd := &cobra.Command{ + cmd := setupTtModuleCmd(internalReplicasetDowngradeModule, &cobra.Command{ Use: "downgrade ( | ) [flags]\n\n" + replicasetUriHelp, DisableFlagsInUseLine: true, Short: "Downgrade tarantool cluster", Long: "Downgrade tarantool cluster.\n\n" + libconnect.EnvCredentialsHelp + "\n\n", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalReplicasetDowngradeModule, args) - util.HandleCmdErr(cmd, err) - }, Args: cobra.ExactArgs(1), - } + }) cmd.Flags().StringArrayVarP(&chosenReplicasetAliases, "replicaset", "r", []string{}, "specify the replicaset name(s) to downgrade") @@ -121,7 +108,7 @@ func newDowngradeCmd() *cobra.Command { // newStatusCmd creates a "replicaset status" command. func newStatusCmd() *cobra.Command { - cmd := &cobra.Command{ + cmd := setupTtModuleCmd(internalReplicasetStatusModule, &cobra.Command{ Use: "status [--cartridge|--config|--custom] [flags] " + "( | | )\n\n" + replicasetUriHelp, @@ -129,14 +116,8 @@ func newStatusCmd() *cobra.Command { Short: "Show a replicaset status", Long: "Show a replicaset status.\n\n" + libconnect.EnvCredentialsHelp + "\n\n", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalReplicasetStatusModule, args) - util.HandleCmdErr(cmd, err) - }, Args: cobra.ExactArgs(1), - } + }) addOrchestratorFlags(cmd) addTarantoolConnectFlags(cmd) @@ -145,7 +126,7 @@ func newStatusCmd() *cobra.Command { // newPromoteCmd creates a "replicaset promote" command. func newPromoteCmd() *cobra.Command { - cmd := &cobra.Command{ + cmd := setupTtModuleCmd(internalReplicasetPromoteModule, &cobra.Command{ Use: "promote [--cartridge|--config|--custom] [-f] [--timeout secs] [flags] " + "( | )\n\n" + replicasetUriHelp, @@ -153,14 +134,8 @@ func newPromoteCmd() *cobra.Command { Short: "Promote an instance", Long: "Promote an instance.\n\n" + libconnect.EnvCredentialsHelp + "\n\n", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalReplicasetPromoteModule, args) - util.HandleCmdErr(cmd, err) - }, Args: cobra.ExactArgs(1), - } + }) addOrchestratorFlags(cmd) addTarantoolConnectFlags(cmd) @@ -177,19 +152,13 @@ func newPromoteCmd() *cobra.Command { // newDemoteCmd creates a "replicaset demote" command. func newDemoteCmd() *cobra.Command { - cmd := &cobra.Command{ + cmd := setupTtModuleCmd(internalReplicasetDemoteModule, &cobra.Command{ Use: "demote [-f] [--timeout secs] [flags] ", DisableFlagsInUseLine: true, Short: "Demote an instance", Long: "Demote an instance.", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalReplicasetDemoteModule, args) - util.HandleCmdErr(cmd, err) - }, - Args: cobra.ExactArgs(1), - } + Args: cobra.ExactArgs(1), + }) addOrchestratorFlags(cmd) cmd.Flags().BoolVarP(&replicasetForce, "force", "f", false, @@ -201,19 +170,13 @@ func newDemoteCmd() *cobra.Command { // newExpelCmd creates a "replicaset expel" command. func newExpelCmd() *cobra.Command { - cmd := &cobra.Command{ + cmd := setupTtModuleCmd(internalReplicasetExpelModule, &cobra.Command{ Use: "expel [-f] [--cartridge|--config|--custom] [--timeout secs] " + "", Short: "Expel an instance from a replicaset", Long: "Expel an instance from a replicaset.", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalReplicasetExpelModule, args) - util.HandleCmdErr(cmd, err) - }, - Args: cobra.ExactArgs(1), - } + Args: cobra.ExactArgs(1), + }) addOrchestratorFlags(cmd) cmd.Flags().BoolVarP(&replicasetForce, "force", "f", false, @@ -226,18 +189,12 @@ func newExpelCmd() *cobra.Command { // newBootstrapCmd creates a "replicaset bootstrap" command. func newBootstrapCmd() *cobra.Command { - cmd := &cobra.Command{ + cmd := setupTtModuleCmd(internalReplicasetBootstrapModule, &cobra.Command{ Use: "bootstrap [--timeout secs] [flags] ", Short: "Bootstrap an application or instance", Long: "Bootstrap an application or instance.", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalReplicasetBootstrapModule, args) - util.HandleCmdErr(cmd, err) - }, - Args: cobra.ExactArgs(1), - } + Args: cobra.ExactArgs(1), + }) addOrchestratorFlags(cmd) cmd.Flags().BoolVarP(&replicasetBootstrapVshard, "bootstrap-vshard", "", false, @@ -254,7 +211,7 @@ func newBootstrapCmd() *cobra.Command { // newBootstrapVShardCmd creates a "vshard bootstrap" command. func newBootstrapVShardCmd() *cobra.Command { - cmd := &cobra.Command{ + cmd := setupTtModuleCmd(internalReplicasetBootstrapVShardModule, &cobra.Command{ Use: "bootstrap [--cartridge|--config|--custom] [--timeout secs] [flags] " + "( | | )\n\n" + replicasetUriHelp, @@ -262,14 +219,8 @@ func newBootstrapVShardCmd() *cobra.Command { Short: "Bootstrap vshard in the cluster", Long: "Bootstrap vshard in the cluster.\n\n" + libconnect.EnvCredentialsHelp + "\n\n", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalReplicasetBootstrapVShardModule, args) - util.HandleCmdErr(cmd, err) - }, Args: cobra.ExactArgs(1), - } + }) addOrchestratorFlags(cmd) addTarantoolConnectFlags(cmd) @@ -294,18 +245,12 @@ func newVShardCmd() *cobra.Command { // newRebootstrapCmd creates a "replicaset rebootstrap" command. func newRebootstrapCmd() *cobra.Command { - cmd := &cobra.Command{ + cmd := setupTtModuleCmd(internalReplicasetRebootstrapModule, &cobra.Command{ Use: "rebootstrap ", DisableFlagsInUseLine: true, Short: "Re-bootstraps an instance", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalReplicasetRebootstrapModule, args) - util.HandleCmdErr(cmd, err) - }, - Args: cobra.ExactArgs(1), - } + Args: cobra.ExactArgs(1), + }) cmd.Flags().BoolVarP(&rebootstrapConfirmed, "yes", "y", false, "automatically confirm rebootstrap") @@ -326,19 +271,13 @@ func newRolesCmd() *cobra.Command { // newRolesAddCmd creates a "replicaset roles add" command. func newRolesAddCmd() *cobra.Command { - cmd := &cobra.Command{ + cmd := setupTtModuleCmd(internalReplicasetRolesAddModule, &cobra.Command{ Use: "add [--cartridge|--config|--custom] [-f] [--timeout secs]" + " [flags]", Short: "Adds a role for Cartridge and Tarantool 3 orchestrator", Long: "Adds a role for Cartridge and Tarantool 3 orchestrator", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalReplicasetRolesAddModule, args) - util.HandleCmdErr(cmd, err) - }, - Args: cobra.ExactArgs(2), - } + Args: cobra.ExactArgs(2), + }) cmd.Flags().StringVarP(&replicasetReplicasetName, "replicaset", "r", "", "name of a target replicaset") @@ -364,19 +303,13 @@ func newRolesAddCmd() *cobra.Command { // newRolesRemoveCmd creates a "replicaset roles remove" command. func newRolesRemoveCmd() *cobra.Command { - cmd := &cobra.Command{ + cmd := setupTtModuleCmd(internalReplicasetRolesRemoveModule, &cobra.Command{ Use: "remove [--cartridge|--config|--custom] [-f] [--timeout secs]" + " [flags]", Short: "Removes a role for Cartridge and Tarantool 3 orchestrator", Long: "Removes a role for Cartridge and Tarantool 3 orchestrator", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalReplicasetRolesRemoveModule, args) - util.HandleCmdErr(cmd, err) - }, - Args: cobra.ExactArgs(2), - } + Args: cobra.ExactArgs(2), + }) cmd.Flags().StringVarP(&replicasetReplicasetName, "replicaset", "r", "", "name of a target replicaset") diff --git a/cli/cmd/restart.go b/cli/cmd/restart.go index 65264d0df..bfd7940a9 100644 --- a/cli/cmd/restart.go +++ b/cli/cmd/restart.go @@ -8,7 +8,6 @@ import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmd/internal" "github.com/tarantool/tt/cli/cmdcontext" - "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/running" "github.com/tarantool/tt/cli/util" ) @@ -19,16 +18,10 @@ var ( // NewRestartCmd creates start command. func NewRestartCmd() *cobra.Command { - var restartCmd = &cobra.Command{ + var restartCmd = setupTtModuleCmd(internalRestartModule, &cobra.Command{ Use: "restart [ | ]", Short: "Restart tarantool instance(s)", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalRestartModule, args) - util.HandleCmdErr(cmd, err) - }, - Args: cobra.RangeArgs(0, 1), + Args: cobra.RangeArgs(0, 1), ValidArgsFunction: func( cmd *cobra.Command, args []string, @@ -38,7 +31,7 @@ func NewRestartCmd() *cobra.Command { running.ExtractActiveAppNames, running.ExtractActiveInstanceNames) }, - } + }) restartCmd.Flags().BoolVarP(&autoYes, "yes", "y", false, `Automatic yes to confirmation prompt`) diff --git a/cli/cmd/rocks.go b/cli/cmd/rocks.go index 794041569..147a9aaf6 100644 --- a/cli/cmd/rocks.go +++ b/cli/cmd/rocks.go @@ -3,26 +3,18 @@ package cmd import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmdcontext" - "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/rocks" - "github.com/tarantool/tt/cli/util" ) // NewRocksCmd creates rocks command. func NewRocksCmd() *cobra.Command { - var rocksCmd = &cobra.Command{ + var rocksCmd = setupTtModuleCmd(internalRocksModule, &cobra.Command{ Use: "rocks", Short: "LuaRocks package manager", // Disabled all flags parsing on this commands leaf. // LuaRocks will handle it self. DisableFlagParsing: true, - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalRocksModule, args) - util.HandleCmdErr(cmd, err) - }, - } + }) return rocksCmd } diff --git a/cli/cmd/start.go b/cli/cmd/start.go index 9ef374529..610e15476 100644 --- a/cli/cmd/start.go +++ b/cli/cmd/start.go @@ -13,10 +13,8 @@ import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmd/internal" "github.com/tarantool/tt/cli/cmdcontext" - "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/running" "github.com/tarantool/tt/cli/tail" - "github.com/tarantool/tt/cli/util" "github.com/tarantool/tt/lib/integrity" ) @@ -36,15 +34,9 @@ var ( // NewStartCmd creates start command. func NewStartCmd() *cobra.Command { - var startCmd = &cobra.Command{ + var startCmd = setupTtModuleCmd(internalStartModule, &cobra.Command{ Use: "start [ | ]", Short: "Start tarantool instance(s)", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalStartModule, args) - util.HandleCmdErr(cmd, err) - }, ValidArgsFunction: func( cmd *cobra.Command, args []string, @@ -54,7 +46,7 @@ func NewStartCmd() *cobra.Command { running.ExtractInactiveAppNames, running.ExtractInactiveInstanceNames) }, - } + }) startCmd.Flags().BoolVar(&watchdog, "watchdog", false, "") startCmd.Flags().MarkHidden("watchdog") diff --git a/cli/cmd/status.go b/cli/cmd/status.go index afa332a06..f77201723 100644 --- a/cli/cmd/status.go +++ b/cli/cmd/status.go @@ -4,17 +4,15 @@ import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmd/internal" "github.com/tarantool/tt/cli/cmdcontext" - "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/running" "github.com/tarantool/tt/cli/status" - "github.com/tarantool/tt/cli/util" ) var opts status.StatusOpts // NewStatusCmd creates status command. func NewStatusCmd() *cobra.Command { - var statusCmd = &cobra.Command{ + var statusCmd = setupTtModuleCmd(internalStatusModule, &cobra.Command{ Use: "status [ | ]", Short: "Status of the tarantool instance(s)", Long: `The 'status' command provides information about the status of Tarantool instances. @@ -32,12 +30,6 @@ Columns: - CONFIG: The config info status (for Tarantool 3+). - BOX: The box info status. - UPSTREAM: The replication upstream status.`, - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalStatusModule, args) - util.HandleCmdErr(cmd, err) - }, ValidArgsFunction: func( cmd *cobra.Command, args []string, @@ -47,7 +39,7 @@ Columns: running.ExtractAppNames, running.ExtractInstanceNames) }, - } + }) statusCmd.Flags().BoolVarP(&opts.Pretty, "pretty", "p", false, "pretty-print table") statusCmd.Flags().BoolVarP(&opts.Details, "details", "d", false, "print detailed alerts.") diff --git a/cli/cmd/stop.go b/cli/cmd/stop.go index 65a6a48b8..30ef3f717 100644 --- a/cli/cmd/stop.go +++ b/cli/cmd/stop.go @@ -2,28 +2,22 @@ package cmd import ( "fmt" + "os" + "github.com/apex/log" "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmd/internal" "github.com/tarantool/tt/cli/cmdcontext" - "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/running" "github.com/tarantool/tt/cli/util" - "os" ) // NewStopCmd creates stop command. func NewStopCmd() *cobra.Command { - var stopCmd = &cobra.Command{ + var stopCmd = setupTtModuleCmd(internalStopWithConfirmationModule, &cobra.Command{ Use: "stop [ | ]", Short: "Stop tarantool instance(s)", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - internalStopWithConfirmationModule, args) - util.HandleCmdErr(cmd, err) - }, - Args: cobra.RangeArgs(0, 1), + Args: cobra.RangeArgs(0, 1), ValidArgsFunction: func( cmd *cobra.Command, args []string, @@ -33,7 +27,7 @@ func NewStopCmd() *cobra.Command { running.ExtractActiveAppNames, running.ExtractActiveInstanceNames) }, - } + }) stopCmd.Flags().BoolVarP(&autoYes, "yes", "y", false, `Automatic yes to confirmation prompt`) diff --git a/cli/cmd/uninstall.go b/cli/cmd/uninstall.go index ccf67c963..15eccfc27 100644 --- a/cli/cmd/uninstall.go +++ b/cli/cmd/uninstall.go @@ -5,22 +5,14 @@ import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmdcontext" - "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/uninstall" - "github.com/tarantool/tt/cli/util" ) // newUninstallTtCmd creates a command to install tt. func newUninstallTtCmd() *cobra.Command { - var tntCmd = &cobra.Command{ + var tntCmd = setupTtModuleCmd(InternalUninstallModule, &cobra.Command{ Use: "tt [version]", Short: "Uninstall tt", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - InternalUninstallModule, args) - util.HandleCmdErr(cmd, err) - }, ValidArgsFunction: func( cmd *cobra.Command, args []string, @@ -31,22 +23,16 @@ func newUninstallTtCmd() *cobra.Command { return uninstall.GetList(cliOpts, cmd.Name()), cobra.ShellCompDirectiveNoFileComp }, - } + }) return tntCmd } // newUninstallTarantoolCmd creates a command to install tarantool. func newUninstallTarantoolCmd() *cobra.Command { - var tntCmd = &cobra.Command{ + var tntCmd = setupTtModuleCmd(InternalUninstallModule, &cobra.Command{ Use: "tarantool [version]", Short: "Uninstall tarantool community edition", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - InternalUninstallModule, args) - util.HandleCmdErr(cmd, err) - }, ValidArgsFunction: func( cmd *cobra.Command, args []string, @@ -57,22 +43,16 @@ func newUninstallTarantoolCmd() *cobra.Command { return uninstall.GetList(cliOpts, cmd.Name()), cobra.ShellCompDirectiveNoFileComp }, - } + }) return tntCmd } // newUninstallTarantoolEeCmd creates a command to install tarantool-ee. func newUninstallTarantoolEeCmd() *cobra.Command { - var tntCmd = &cobra.Command{ + var tntCmd = setupTtModuleCmd(InternalUninstallModule, &cobra.Command{ Use: "tarantool-ee [version]", Short: "Uninstall tarantool enterprise edition", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - InternalUninstallModule, args) - util.HandleCmdErr(cmd, err) - }, ValidArgsFunction: func( cmd *cobra.Command, args []string, @@ -83,23 +63,17 @@ func newUninstallTarantoolEeCmd() *cobra.Command { return uninstall.GetList(cliOpts, cmd.Name()), cobra.ShellCompDirectiveNoFileComp }, - } + }) return tntCmd } // newUninstallTarantoolDevCmd creates a command to uninstall tarantool-dev. func newUninstallTarantoolDevCmd() *cobra.Command { - tntCmd := &cobra.Command{ + tntCmd := setupTtModuleCmd(InternalUninstallModule, &cobra.Command{ Use: "tarantool-dev", Short: "Uninstall tarantool-dev", - Run: func(cmd *cobra.Command, args []string) { - cmdCtx.CommandName = cmd.Name() - err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, - InternalUninstallModule, args) - util.HandleCmdErr(cmd, err) - }, - } + }) return tntCmd }