Skip to content

Commit

Permalink
utils: extract common handler
Browse files Browse the repository at this point in the history
Extract handleCmdErr to utils so it can be reused in tt-ee commands.
  • Loading branch information
DifferentialOrange authored and psergee committed May 27, 2024
1 parent 7f79055 commit 22ee261
Show file tree
Hide file tree
Showing 33 changed files with 91 additions and 70 deletions.
5 changes: 3 additions & 2 deletions cli/cmd/binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/cfg_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
},
}

Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
}
Expand All @@ -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),
}
Expand All @@ -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),
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 0 additions & 18 deletions cli/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'" +
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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: `
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions cli/cmd/coredump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions cli/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -27,7 +28,7 @@ func NewDaemonCmd() *cobra.Command {

err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalDaemonStartModule, args)
handleCmdErr(cmd, err)
util.HandleCmdErr(cmd, err)
},
}

Expand All @@ -41,7 +42,7 @@ func NewDaemonCmd() *cobra.Command {

err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalDaemonStopModule, args)
handleCmdErr(cmd, err)
util.HandleCmdErr(cmd, err)
},
}

Expand All @@ -55,7 +56,7 @@ func NewDaemonCmd() *cobra.Command {

err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalDaemonStatusModule, args)
handleCmdErr(cmd, err)
util.HandleCmdErr(cmd, err)
},
}

Expand All @@ -69,7 +70,7 @@ func NewDaemonCmd() *cobra.Command {

err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalDaemonRestartModule, args)
handleCmdErr(cmd, err)
util.HandleCmdErr(cmd, err)
},
}

Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
},
}

Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
},
}

Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
},
}

Expand Down
9 changes: 5 additions & 4 deletions cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
},
}

Expand All @@ -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)
},
}

Expand All @@ -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)
},
}

Expand All @@ -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)
},
}

Expand Down
Loading

0 comments on commit 22ee261

Please sign in to comment.