diff --git a/cli/cmd/binaries.go b/cli/cmd/binaries.go index 0510ec613..2248d77ab 100644 --- a/cli/cmd/binaries.go +++ b/cli/cmd/binaries.go @@ -8,6 +8,7 @@ import ( "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" ) @@ -39,7 +40,7 @@ You will need to choose version using arrow keys in your console. Run: func(cmd *cobra.Command, args []string) { err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalSwitchModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } var listCmd = &cobra.Command{ @@ -48,7 +49,7 @@ You will need to choose version using arrow keys in your console. Run: func(cmd *cobra.Command, args []string) { err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalListModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } binariesCmd.AddCommand(switchCmd) diff --git a/cli/cmd/build.go b/cli/cmd/build.go index 2bfae403d..296705ec9 100644 --- a/cli/cmd/build.go +++ b/cli/cmd/build.go @@ -6,6 +6,7 @@ import ( "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 ( @@ -21,7 +22,7 @@ func NewBuildCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalBuildModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.MaximumNArgs(1), ValidArgsFunction: func( diff --git a/cli/cmd/cat.go b/cli/cmd/cat.go index 7c75008d3..795e54214 100644 --- a/cli/cmd/cat.go +++ b/cli/cmd/cat.go @@ -36,7 +36,7 @@ func NewCatCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalCatModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/cmd/cfg_dump.go b/cli/cmd/cfg_dump.go index 933857736..10cf56eb0 100644 --- a/cli/cmd/cfg_dump.go +++ b/cli/cmd/cfg_dump.go @@ -7,6 +7,7 @@ import ( "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 ( @@ -22,7 +23,7 @@ func NewDumpCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalDumpModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/cmd/check.go b/cli/cmd/check.go index deb30a524..db44d54e2 100644 --- a/cli/cmd/check.go +++ b/cli/cmd/check.go @@ -6,6 +6,7 @@ import ( "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. @@ -17,7 +18,7 @@ func NewCheckCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalCheckModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/cmd/clean.go b/cli/cmd/clean.go index e47fffb62..1847abb96 100644 --- a/cli/cmd/clean.go +++ b/cli/cmd/clean.go @@ -25,7 +25,7 @@ func NewCleanCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalCleanModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, ValidArgsFunction: func( cmd *cobra.Command, diff --git a/cli/cmd/cluster.go b/cli/cmd/cluster.go index ac8fb1da8..5ba637d18 100644 --- a/cli/cmd/cluster.go +++ b/cli/cmd/cluster.go @@ -15,6 +15,7 @@ import ( "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/running" + "github.com/tarantool/tt/cli/util" libcluster "github.com/tarantool/tt/lib/cluster" libconnect "github.com/tarantool/tt/lib/connect" "github.com/tarantool/tt/lib/integrity" @@ -103,7 +104,7 @@ func newClusterReplicasetCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalClusterReplicasetPromoteModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.ExactArgs(2), } @@ -124,7 +125,7 @@ func newClusterReplicasetCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalClusterReplicasetDemoteModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.ExactArgs(2), } @@ -146,7 +147,7 @@ func newClusterReplicasetCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalClusterReplicasetExpelModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.ExactArgs(2), } @@ -185,7 +186,7 @@ func NewClusterCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalClusterShowModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.ExactArgs(1), ValidArgsFunction: func( @@ -232,7 +233,7 @@ func NewClusterCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalClusterPublishModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.ExactArgs(2), ValidArgsFunction: func( diff --git a/cli/cmd/common.go b/cli/cmd/common.go index 517b49fa6..d851991a3 100644 --- a/cli/cmd/common.go +++ b/cli/cmd/common.go @@ -4,31 +4,13 @@ import ( "errors" "fmt" "io" - "os" - "github.com/apex/log" - "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/configure" - "github.com/tarantool/tt/cli/util" libcluster "github.com/tarantool/tt/lib/cluster" "github.com/tarantool/tt/lib/integrity" ) -// handleCmdErr handles an error returned by command implementation. -// If received error is of an ArgError type, usage help is printed. -func handleCmdErr(cmd *cobra.Command, err error) { - if err != nil { - var argError *util.ArgError - if errors.As(err, &argError) { - log.Error(argError.Error()) - cmd.Usage() - os.Exit(1) - } - log.Fatalf(err.Error()) - } -} - // errNoConfig is returned if environment config file tt.yaml not found. var errNoConfig = errors.New(configure.ConfigName + " not found, you need to create tt environment config with 'tt init'" + diff --git a/cli/cmd/completion.go b/cli/cmd/completion.go index 70a124d9a..936a830ce 100644 --- a/cli/cmd/completion.go +++ b/cli/cmd/completion.go @@ -9,6 +9,7 @@ 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. @@ -22,7 +23,7 @@ func NewCompletionCmd() *cobra.Command { args = modules.GetDefaultCmdArgs(cmd.Name()) err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalCompletionCmd, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), Example: ` diff --git a/cli/cmd/connect.go b/cli/cmd/connect.go index c68de4a46..73c9ceb8d 100644 --- a/cli/cmd/connect.go +++ b/cli/cmd/connect.go @@ -64,7 +64,7 @@ func NewConnectCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalConnectModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.MinimumNArgs(1), ValidArgsFunction: func( diff --git a/cli/cmd/coredump.go b/cli/cmd/coredump.go index 222469cd4..bb5638c03 100644 --- a/cli/cmd/coredump.go +++ b/cli/cmd/coredump.go @@ -3,6 +3,7 @@ package cmd import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/coredump" + "github.com/tarantool/tt/cli/util" ) // NewCoredumpCmd creates coredump command. @@ -17,7 +18,7 @@ func NewCoredumpCmd() *cobra.Command { Short: "pack tarantool coredump into tar.gz archive", Run: func(cmd *cobra.Command, args []string) { if err := coredump.Pack(args[0]); err != nil { - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) } }, Args: cobra.ExactArgs(1), @@ -28,7 +29,7 @@ func NewCoredumpCmd() *cobra.Command { Short: "unpack tarantool coredump tar.gz archive", Run: func(cmd *cobra.Command, args []string) { if err := coredump.Unpack(args[0]); err != nil { - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) } }, Args: cobra.ExactArgs(1), @@ -40,7 +41,7 @@ func NewCoredumpCmd() *cobra.Command { Short: "inspect tarantool coredump", Run: func(cmd *cobra.Command, args []string) { if err := coredump.Inspect(args[0], sourceDir); err != nil { - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) } }, Args: cobra.ExactArgs(1), diff --git a/cli/cmd/create.go b/cli/cmd/create.go index cd874969e..841a2c10d 100644 --- a/cli/cmd/create.go +++ b/cli/cmd/create.go @@ -35,7 +35,7 @@ func NewCreateCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalCreateModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { diff --git a/cli/cmd/daemon.go b/cli/cmd/daemon.go index 9a21c4fd6..a1a15a955 100644 --- a/cli/cmd/daemon.go +++ b/cli/cmd/daemon.go @@ -8,6 +8,7 @@ import ( "github.com/tarantool/tt/cli/daemon" "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/process_utils" + "github.com/tarantool/tt/cli/util" ) // NewDaemonCmd creates daemon command. @@ -27,7 +28,7 @@ func NewDaemonCmd() *cobra.Command { err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalDaemonStartModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } @@ -41,7 +42,7 @@ func NewDaemonCmd() *cobra.Command { err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalDaemonStopModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } @@ -55,7 +56,7 @@ func NewDaemonCmd() *cobra.Command { err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalDaemonStatusModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } @@ -69,7 +70,7 @@ func NewDaemonCmd() *cobra.Command { err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalDaemonRestartModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/cmd/download.go b/cli/cmd/download.go index 110d54a61..4272aab3f 100644 --- a/cli/cmd/download.go +++ b/cli/cmd/download.go @@ -5,6 +5,7 @@ import ( "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 ( @@ -25,7 +26,7 @@ func NewDownloadCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalDownloadModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/cmd/env.go b/cli/cmd/env.go index b7a70da96..e40b2c65f 100644 --- a/cli/cmd/env.go +++ b/cli/cmd/env.go @@ -7,6 +7,7 @@ import ( "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/env" "github.com/tarantool/tt/cli/modules" + "github.com/tarantool/tt/cli/util" ) // NewEnvCmd creates env command. @@ -19,7 +20,7 @@ func NewEnvCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalEnvModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/cmd/init.go b/cli/cmd/init.go index 339a3b467..0907ccafe 100644 --- a/cli/cmd/init.go +++ b/cli/cmd/init.go @@ -8,6 +8,7 @@ import ( "github.com/tarantool/tt/cli/configure" init_pkg "github.com/tarantool/tt/cli/init" "github.com/tarantool/tt/cli/modules" + "github.com/tarantool/tt/cli/util" ) var initCtx init_pkg.InitCtx @@ -22,7 +23,7 @@ func NewInitCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalInitModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/cmd/install.go b/cli/cmd/install.go index cdf95449e..3de043c43 100644 --- a/cli/cmd/install.go +++ b/cli/cmd/install.go @@ -5,6 +5,7 @@ import ( "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 @@ -19,7 +20,7 @@ func newInstallTtCmd() *cobra.Command { installCtx.ProgramName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalInstallModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } @@ -36,7 +37,7 @@ func newInstallTarantoolCmd() *cobra.Command { installCtx.ProgramName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalInstallModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } @@ -58,7 +59,7 @@ func newInstallTarantoolEeCmd() *cobra.Command { installCtx.ProgramName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalInstallModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } @@ -83,7 +84,7 @@ func newInstallTarantoolDevCmd() *cobra.Command { installCtx.ProgramName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalInstallModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/cmd/instances.go b/cli/cmd/instances.go index b80f6d27b..272f54502 100644 --- a/cli/cmd/instances.go +++ b/cli/cmd/instances.go @@ -5,6 +5,7 @@ import ( "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. @@ -16,7 +17,7 @@ func NewInstancesCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalInstancesModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.ExactArgs(0), } diff --git a/cli/cmd/kill.go b/cli/cmd/kill.go index 6058b5332..61deb22d1 100644 --- a/cli/cmd/kill.go +++ b/cli/cmd/kill.go @@ -26,7 +26,7 @@ func NewKillCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalKillModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, ValidArgsFunction: func( cmd *cobra.Command, diff --git a/cli/cmd/logrotate.go b/cli/cmd/logrotate.go index bba5d1779..87bb7e9b9 100644 --- a/cli/cmd/logrotate.go +++ b/cli/cmd/logrotate.go @@ -7,6 +7,7 @@ import ( "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. @@ -18,7 +19,7 @@ func NewLogrotateCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalLogrotateModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, ValidArgsFunction: func( cmd *cobra.Command, diff --git a/cli/cmd/pack.go b/cli/cmd/pack.go index 5e96287c0..642e21f89 100644 --- a/cli/cmd/pack.go +++ b/cli/cmd/pack.go @@ -9,6 +9,7 @@ import ( "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/pack" + "github.com/tarantool/tt/cli/util" "github.com/tarantool/tt/lib/integrity" ) @@ -44,7 +45,7 @@ The supported types are: tgz, deb, rpm`, } cmdCtx.CommandName = cmd.Name() err = modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalPackModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/cmd/play.go b/cli/cmd/play.go index fb3425975..c3753851d 100644 --- a/cli/cmd/play.go +++ b/cli/cmd/play.go @@ -44,7 +44,7 @@ func NewPlayCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalPlayModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/cmd/replicaset.go b/cli/cmd/replicaset.go index 2a1af374b..71b610eab 100644 --- a/cli/cmd/replicaset.go +++ b/cli/cmd/replicaset.go @@ -13,6 +13,7 @@ import ( "github.com/tarantool/tt/cli/replicaset" replicasetcmd "github.com/tarantool/tt/cli/replicaset/cmd" "github.com/tarantool/tt/cli/running" + "github.com/tarantool/tt/cli/util" libconnect "github.com/tarantool/tt/lib/connect" "github.com/tarantool/tt/lib/integrity" ) @@ -57,7 +58,7 @@ func newStatusCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalReplicasetStatusModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.ExactArgs(1), } @@ -81,7 +82,7 @@ func newPromoteCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalReplicasetPromoteModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.ExactArgs(1), } @@ -110,7 +111,7 @@ func newDemoteCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalReplicasetDemoteModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.ExactArgs(1), } @@ -134,7 +135,7 @@ func newExpelCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalReplicasetExpelModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.ExactArgs(1), } diff --git a/cli/cmd/restart.go b/cli/cmd/restart.go index 048fb7772..65264d0df 100644 --- a/cli/cmd/restart.go +++ b/cli/cmd/restart.go @@ -26,7 +26,7 @@ func NewRestartCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalRestartModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Args: cobra.RangeArgs(0, 1), ValidArgsFunction: func( diff --git a/cli/cmd/rocks.go b/cli/cmd/rocks.go index 2fedeb6bd..794041569 100644 --- a/cli/cmd/rocks.go +++ b/cli/cmd/rocks.go @@ -5,6 +5,7 @@ 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" ) // NewRocksCmd creates rocks command. @@ -19,7 +20,7 @@ func NewRocksCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalRocksModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/cmd/run.go b/cli/cmd/run.go index 2e8e93b38..bc21c57e0 100644 --- a/cli/cmd/run.go +++ b/cli/cmd/run.go @@ -5,6 +5,7 @@ import ( "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 ( @@ -37,7 +38,7 @@ are passed after '--'. } } err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalRunModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, Example: ` # Print current environment Tarantool version: diff --git a/cli/cmd/search.go b/cli/cmd/search.go index ff93c8046..0fd86308f 100644 --- a/cli/cmd/search.go +++ b/cli/cmd/search.go @@ -5,6 +5,7 @@ import ( "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/search" + "github.com/tarantool/tt/cli/util" ) var ( @@ -24,7 +25,7 @@ func newSearchTtCmd() *cobra.Command { searchCtx.ProgramName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalSearchModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } @@ -40,7 +41,7 @@ func newSearchTarantoolCmd() *cobra.Command { searchCtx.ProgramName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalSearchModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } @@ -56,7 +57,7 @@ func newSearchTarantoolEeCmd() *cobra.Command { searchCtx.ProgramName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalSearchModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } tntCmd.Flags().BoolVar(&debug, "debug", debug, diff --git a/cli/cmd/start.go b/cli/cmd/start.go index ddb0aaf52..e4127486f 100644 --- a/cli/cmd/start.go +++ b/cli/cmd/start.go @@ -15,6 +15,7 @@ import ( "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" "github.com/tarantool/tt/lib/integrity" ) @@ -37,7 +38,7 @@ func NewStartCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalStartModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, ValidArgsFunction: func( cmd *cobra.Command, diff --git a/cli/cmd/status.go b/cli/cmd/status.go index 0270d8dfa..e3cd242a7 100644 --- a/cli/cmd/status.go +++ b/cli/cmd/status.go @@ -7,6 +7,7 @@ import ( "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 @@ -20,7 +21,7 @@ func NewStatusCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalStatusModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, ValidArgsFunction: func( cmd *cobra.Command, diff --git a/cli/cmd/stop.go b/cli/cmd/stop.go index ca3180ae0..54da5b005 100644 --- a/cli/cmd/stop.go +++ b/cli/cmd/stop.go @@ -7,6 +7,7 @@ import ( "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/running" + "github.com/tarantool/tt/cli/util" ) // NewStopCmd creates stop command. @@ -18,7 +19,7 @@ func NewStopCmd() *cobra.Command { cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalStopModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, ValidArgsFunction: func( cmd *cobra.Command, diff --git a/cli/cmd/uninstall.go b/cli/cmd/uninstall.go index 2e99f3df6..219d2dd35 100644 --- a/cli/cmd/uninstall.go +++ b/cli/cmd/uninstall.go @@ -7,6 +7,7 @@ import ( "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/uninstall" + "github.com/tarantool/tt/cli/util" ) var ( @@ -23,7 +24,7 @@ func newUninstallTtCmd() *cobra.Command { programName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, InternalUninstallModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, ValidArgsFunction: func( cmd *cobra.Command, @@ -50,7 +51,7 @@ func newUninstallTarantoolCmd() *cobra.Command { programName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, InternalUninstallModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, ValidArgsFunction: func( cmd *cobra.Command, @@ -77,7 +78,7 @@ func newUninstallTarantoolEeCmd() *cobra.Command { programName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, InternalUninstallModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, ValidArgsFunction: func( cmd *cobra.Command, @@ -104,7 +105,7 @@ func newUninstallTarantoolDevCmd() *cobra.Command { programName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, InternalUninstallModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/cmd/version.go b/cli/cmd/version.go index f8e297451..5801eeb89 100644 --- a/cli/cmd/version.go +++ b/cli/cmd/version.go @@ -6,6 +6,7 @@ import ( "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmdcontext" "github.com/tarantool/tt/cli/modules" + "github.com/tarantool/tt/cli/util" "github.com/tarantool/tt/cli/version" ) @@ -24,7 +25,7 @@ func NewVersionCmd() *cobra.Command { args = modules.GetDefaultCmdArgs(cmd.Name()) err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalVersionModule, args) - handleCmdErr(cmd, err) + util.HandleCmdErr(cmd, err) }, } diff --git a/cli/util/util.go b/cli/util/util.go index 963c33ce4..022506e42 100644 --- a/cli/util/util.go +++ b/cli/util/util.go @@ -5,6 +5,7 @@ import ( "bufio" "bytes" "compress/gzip" + "errors" "fmt" "io" "io/fs" @@ -910,3 +911,17 @@ func Min[T constraints.Ordered](a, b T) T { } return b } + +// HandleCmdErr handles an error returned by command implementation. +// If received error is of an ArgError type, usage help is printed. +func HandleCmdErr(cmd *cobra.Command, err error) { + if err != nil { + var argError *ArgError + if errors.As(err, &argError) { + log.Error(argError.Error()) + cmd.Usage() + os.Exit(1) + } + log.Fatalf(err.Error()) + } +}