Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove code duplication in Command.Run functions #1077

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions cli/cmd/aeon.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
aeoncmd "github.com/tarantool/tt/cli/aeon/cmd"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/console"
"github.com/tarantool/tt/cli/modules"
"github.com/tarantool/tt/cli/running"
"github.com/tarantool/tt/cli/util"
"github.com/tarantool/tt/lib/cluster"
Expand All @@ -37,12 +36,7 @@ func newAeonConnectCmd() *cobra.Command {
tt aeon connect http://localhost:50051
tt aeon connect unix://<socket-path>
tt aeon connect /path/to/config INSTANCE_NAME>`,
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalAeonConnect, args)
util.HandleCmdErr(cmd, err)
},
Run: RunModuleFunc(internalAeonConnect),
Args: cobra.MatchAll(cobra.RangeArgs(1, 2), aeonConnectValidateArgs),
}

Expand Down
14 changes: 2 additions & 12 deletions cli/cmd/binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"github.com/spf13/cobra"
"github.com/tarantool/tt/cli/binary"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/modules"
"github.com/tarantool/tt/cli/search"
"github.com/tarantool/tt/cli/util"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -43,21 +41,13 @@ You will need to choose version using arrow keys in your console.
# Switch with program and version.

$ tt binaries switch tarantool 2.10.4`,
Run: func(cmd *cobra.Command, args []string) {
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalSwitchModule, args)
util.HandleCmdErr(cmd, err)
},
Run: RunModuleFunc(internalSwitchModule),
Args: cobra.MatchAll(cobra.MaximumNArgs(2), binariesSwitchValidateArgs),
}
var listCmd = &cobra.Command{
Use: "list",
Short: "Show a list of installed binaries and their versions.",
Run: func(cmd *cobra.Command, args []string) {
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalListModule, args)
util.HandleCmdErr(cmd, err)
},
Run: RunModuleFunc(internalListModule),
}
binariesCmd.AddCommand(switchCmd)
binariesCmd.AddCommand(listCmd)
Expand Down
10 changes: 2 additions & 8 deletions cli/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"github.com/spf13/cobra"
"github.com/tarantool/tt/cli/build"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/modules"
"github.com/tarantool/tt/cli/running"
"github.com/tarantool/tt/cli/util"
)

var (
Expand All @@ -19,12 +17,8 @@ func NewBuildCmd() *cobra.Command {
var buildCmd = &cobra.Command{
Use: "build [<PATH> | <APP_NAME>] [flags]",
Short: `Build an application (default ".")`,
Run: func(cmd *cobra.Command, args []string) {
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalBuildModule, args)
util.HandleCmdErr(cmd, err)
},
Args: cobra.MaximumNArgs(1),
Run: RunModuleFunc(internalBuildModule),
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: func(
cmd *cobra.Command,
args []string,
Expand Down
8 changes: 1 addition & 7 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 @@ -34,12 +33,7 @@ func NewCatCmd() *cobra.Command {
var catCmd = &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)
},
Run: RunModuleFunc(internalCatModule),
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/ " +
Expand Down
9 changes: 1 addition & 8 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 @@ -19,12 +17,7 @@ func NewDumpCmd() *cobra.Command {
var dumpCmd = &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)
},
Run: RunModuleFunc(internalDumpModule),
}

dumpCmd.Flags().BoolVarP(&rawDump, "raw", "r", false,
Expand Down
9 changes: 1 addition & 8 deletions cli/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +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{
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)
},
Run: RunModuleFunc(internalCheckModule),
}

return checkCmd
Expand Down
9 changes: 2 additions & 7 deletions cli/cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/process_utils"
"github.com/tarantool/tt/cli/running"
"github.com/tarantool/tt/cli/util"
Expand All @@ -23,11 +22,7 @@ func NewCleanCmd() *cobra.Command {
var cleanCmd = &cobra.Command{
Use: "clean [INSTANCE_NAME]",
Short: "Clean instance(s) files",
Run: func(cmd *cobra.Command, args []string) {
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalCleanModule,
args)
util.HandleCmdErr(cmd, err)
},
Run: RunModuleFunc(internalCleanModule),
ValidArgsFunction: func(
cmd *cobra.Command,
args []string,
Expand Down Expand Up @@ -83,7 +78,7 @@ func clean(run *running.InstanceCtx) error {
}

if confirm || forceRemove {
for file, _ := range removeFiles {
for file := range removeFiles {
err = os.Remove(file)
if err != nil {
return err
Expand Down
74 changes: 14 additions & 60 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,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 @@ -103,13 +102,8 @@ func newClusterReplicasetCmd() *cobra.Command {
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),
Run: RunModuleFunc(internalClusterReplicasetPromoteModule),
Args: cobra.ExactArgs(2),
}
promoteCmd.Flags().StringVarP(&promoteCtx.Username, "username", "u", "",
"username (used as etcd/tarantool config storage credentials)")
Expand All @@ -124,13 +118,8 @@ func newClusterReplicasetCmd() *cobra.Command {
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),
Run: RunModuleFunc(internalClusterReplicasetDemoteModule),
Args: cobra.ExactArgs(2),
}

demoteCmd.Flags().StringVarP(&demoteCtx.Username, "username", "u", "",
Expand All @@ -146,13 +135,8 @@ func newClusterReplicasetCmd() *cobra.Command {
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),
Run: RunModuleFunc(internalClusterReplicasetExpelModule),
Args: cobra.ExactArgs(2),
}

expelCmd.Flags().StringVarP(&expelCtx.Username, "username", "u", "",
Expand All @@ -172,12 +156,7 @@ func newClusterReplicasetCmd() *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)
},
Run: RunModuleFunc(internalClusterReplicasetRolesAddModule),
Example: "tt cluster replicaset roles add http://user:pass@localhost:3301" +
" roles.metrics-export --instance_name master",
Args: cobra.ExactArgs(2),
Expand All @@ -204,12 +183,7 @@ func newClusterReplicasetCmd() *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)
},
Run: RunModuleFunc(internalClusterReplicasetRolesRemoveModule),
Example: "tt cluster replicaset roles remove http://user:pass@localhost:3301" +
" roles.metrics-export --instance_name master",
Args: cobra.ExactArgs(2),
Expand Down Expand Up @@ -256,13 +230,8 @@ func newClusterFailoverCmd() *cobra.Command {
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),
Run: RunModuleFunc(internalClusterFailoverSwitchModule),
Args: cobra.ExactArgs(2),
}

switchCmd.Flags().StringVarP(&switchCtx.Username, "username", "u", "",
Expand All @@ -279,13 +248,8 @@ func newClusterFailoverCmd() *cobra.Command {
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),
Run: RunModuleFunc(internalClusterFailoverSwitchStatusModule),
Args: cobra.ExactArgs(2),
}

cmd.AddCommand(switchCmd)
Expand All @@ -309,12 +273,7 @@ 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)
},
Run: RunModuleFunc(internalClusterShowModule),
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(
cmd *cobra.Command,
Expand Down Expand Up @@ -356,12 +315,7 @@ 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)
},
Run: RunModuleFunc(internalClusterPublishModule),
Args: cobra.ExactArgs(2),
ValidArgsFunction: func(
cmd *cobra.Command,
Expand Down
13 changes: 13 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,13 @@ func createDataCollectorsAndDataPublishers(ctx integrity.IntegrityCtx,
}
return collectors, publishers, err
}

func RunModuleFunc(internalModule modules.InternalFunc) func(*cobra.Command, []string) {
return 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)
}
}
}
11 changes: 2 additions & 9 deletions cli/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/modules"
"github.com/tarantool/tt/cli/rocks"
"github.com/tarantool/tt/cli/util"
)

// NewCompletionCmd creates a new completion command.
Expand All @@ -18,14 +17,8 @@ func NewCompletionCmd() *cobra.Command {
Use: "completion <SHELL_TYPE>",
Short: "Generate autocomplete for a specified shell. Supported shell type: bash | zsh",
ValidArgs: []string{"bash", "zsh"},
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
args = modules.GetDefaultCmdArgs(cmd.Name())
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalCompletionCmd, args)
util.HandleCmdErr(cmd, err)
},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: RunModuleFunc(internalCompletionCmd),
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Example: `
# Enable auto-completion in current bash shell.

Expand Down
8 changes: 1 addition & 7 deletions cli/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -61,12 +60,7 @@ 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)
},
Run: RunModuleFunc(internalConnectModule),
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: func(
cmd *cobra.Command,
Expand Down
Loading