Skip to content

Commit 06a2340

Browse files
committed
external: move external commands configuration to cmd package
Part of #1136
1 parent ad40524 commit 06a2340

File tree

3 files changed

+83
-76
lines changed

3 files changed

+83
-76
lines changed

cli/cmd/external.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
6+
"github.com/apex/log"
7+
"github.com/spf13/cobra"
8+
"github.com/tarantool/tt/cli/modules"
9+
"github.com/tarantool/tt/cli/util"
10+
)
11+
12+
// ExternalCmd configures external commands.
13+
func configureExternalCmd(rootCmd *cobra.Command,
14+
modulesInfo *modules.ModulesInfo, forceInternal bool, args []string) {
15+
configureExistsCmd(rootCmd, modulesInfo, forceInternal)
16+
configureNonExistentCmd(rootCmd, modulesInfo, args)
17+
}
18+
19+
// configureExistsCmd configures an external commands
20+
// that have internal implementation.
21+
func configureExistsCmd(rootCmd *cobra.Command, modulesInfo *modules.ModulesInfo,
22+
forceInternal bool) {
23+
for _, cmd := range rootCmd.Commands() {
24+
if _, found := (*modulesInfo)[cmd.CommandPath()]; found {
25+
cmd.DisableFlagParsing = !forceInternal
26+
}
27+
}
28+
}
29+
30+
// configureNonExistentCmd configures an external command that
31+
// has no internal implementation within the Tarantool CLI.
32+
func configureNonExistentCmd(rootCmd *cobra.Command,
33+
modulesInfo *modules.ModulesInfo, args []string) {
34+
// Since the user can pass flags, to determine the name of
35+
// an external command we have to take the first non-flag argument.
36+
externalCmd := args[0]
37+
for _, name := range args {
38+
if !strings.HasPrefix(name, "-") && name != "help" {
39+
externalCmd = name
40+
break
41+
}
42+
}
43+
44+
// We avoid overwriting existing commands - we should add a command only
45+
// if it doesn't have an internal implementation in Tarantool CLI.
46+
for _, cmd := range rootCmd.Commands() {
47+
if cmd.Name() == externalCmd {
48+
return
49+
}
50+
}
51+
52+
helpCmd := util.GetHelpCommand(rootCmd)
53+
externalCmdPath := rootCmd.Name() + " " + externalCmd
54+
if _, found := (*modulesInfo)[externalCmdPath]; found {
55+
rootCmd.AddCommand(newExternalCommand(modulesInfo, externalCmd,
56+
externalCmdPath, nil))
57+
helpCmd.AddCommand(newExternalCommand(modulesInfo, externalCmd, externalCmdPath,
58+
[]string{"--help"}))
59+
}
60+
}
61+
62+
// newExternalCommand returns a pointer to a new external
63+
// command that will call modules.RunCmd.
64+
func newExternalCommand(modulesInfo *modules.ModulesInfo,
65+
cmdName, cmdPath string, addArgs []string) *cobra.Command {
66+
cmd := &cobra.Command{
67+
Use: cmdName,
68+
Run: func(cmd *cobra.Command, args []string) {
69+
if addArgs != nil {
70+
args = append(args, addArgs...)
71+
}
72+
73+
cmdCtx.Cli.ForceInternal = false
74+
if err := modules.RunCmd(&cmdCtx, cmdPath, modulesInfo, nil, args); err != nil {
75+
log.Fatalf(err.Error())
76+
}
77+
},
78+
}
79+
80+
cmd.DisableFlagParsing = true
81+
return cmd
82+
}

cli/cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func InitRoot() {
314314
// External commands must be configured in a special way.
315315
// This is necessary, for example, so that we can pass arguments to these commands.
316316
if len(os.Args) > 1 {
317-
configure.ExternalCmd(rootCmd, &cmdCtx, &modulesInfo, cmdCtx.Cli.ForceInternal, os.Args[1:])
317+
configureExternalCmd(rootCmd, &modulesInfo, cmdCtx.Cli.ForceInternal, os.Args[1:])
318318
}
319319

320320
// Configure help command.

cli/configure/configure.go

-75
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ import (
66
"os/exec"
77
"path/filepath"
88
"reflect"
9-
"strings"
109
"syscall"
1110

1211
"github.com/apex/log"
1312
"github.com/mitchellh/mapstructure"
14-
"github.com/spf13/cobra"
1513
"github.com/tarantool/tt/cli/cmdcontext"
1614
"github.com/tarantool/tt/cli/config"
17-
"github.com/tarantool/tt/cli/modules"
1815
"github.com/tarantool/tt/cli/util"
1916
"github.com/tarantool/tt/lib/integrity"
2017
)
@@ -436,78 +433,6 @@ func Cli(cmdCtx *cmdcontext.CmdCtx) error {
436433
return configureDefaultCli(cmdCtx)
437434
}
438435

439-
// ExternalCmd configures external commands.
440-
func ExternalCmd(rootCmd *cobra.Command, cmdCtx *cmdcontext.CmdCtx,
441-
modulesInfo *modules.ModulesInfo, forceInternal bool, args []string) {
442-
configureExistsCmd(rootCmd, modulesInfo, forceInternal)
443-
configureNonExistentCmd(rootCmd, cmdCtx, modulesInfo, args)
444-
}
445-
446-
// configureExistsCmd configures an external commands
447-
// that have internal implementation.
448-
func configureExistsCmd(rootCmd *cobra.Command, modulesInfo *modules.ModulesInfo,
449-
forceInternal bool) {
450-
for _, cmd := range rootCmd.Commands() {
451-
if _, found := (*modulesInfo)[cmd.CommandPath()]; found {
452-
cmd.DisableFlagParsing = !forceInternal
453-
}
454-
}
455-
}
456-
457-
// configureNonExistentCmd configures an external command that
458-
// has no internal implementation within the Tarantool CLI.
459-
func configureNonExistentCmd(rootCmd *cobra.Command, cmdCtx *cmdcontext.CmdCtx,
460-
modulesInfo *modules.ModulesInfo, args []string) {
461-
// Since the user can pass flags, to determine the name of
462-
// an external command we have to take the first non-flag argument.
463-
externalCmd := args[0]
464-
for _, name := range args {
465-
if !strings.HasPrefix(name, "-") && name != "help" {
466-
externalCmd = name
467-
break
468-
}
469-
}
470-
471-
// We avoid overwriting existing commands - we should add a command only
472-
// if it doesn't have an internal implementation in Tarantool CLI.
473-
for _, cmd := range rootCmd.Commands() {
474-
if cmd.Name() == externalCmd {
475-
return
476-
}
477-
}
478-
479-
helpCmd := util.GetHelpCommand(rootCmd)
480-
externalCmdPath := rootCmd.Name() + " " + externalCmd
481-
if _, found := (*modulesInfo)[externalCmdPath]; found {
482-
rootCmd.AddCommand(newExternalCommand(cmdCtx, modulesInfo, externalCmd,
483-
externalCmdPath, nil))
484-
helpCmd.AddCommand(newExternalCommand(cmdCtx, modulesInfo, externalCmd, externalCmdPath,
485-
[]string{"--help"}))
486-
}
487-
}
488-
489-
// newExternalCommand returns a pointer to a new external
490-
// command that will call modules.RunCmd.
491-
func newExternalCommand(cmdCtx *cmdcontext.CmdCtx, modulesInfo *modules.ModulesInfo,
492-
cmdName, cmdPath string, addArgs []string) *cobra.Command {
493-
cmd := &cobra.Command{
494-
Use: cmdName,
495-
Run: func(cmd *cobra.Command, args []string) {
496-
if addArgs != nil {
497-
args = append(args, addArgs...)
498-
}
499-
500-
cmdCtx.Cli.ForceInternal = false
501-
if err := modules.RunCmd(cmdCtx, cmdPath, modulesInfo, nil, args); err != nil {
502-
log.Fatalf(err.Error())
503-
}
504-
},
505-
}
506-
507-
cmd.DisableFlagParsing = true
508-
return cmd
509-
}
510-
511436
// detectLocalTarantool searches for available Tarantool executable.
512437
func detectLocalTarantool(cmdCtx *cmdcontext.CmdCtx, cliOpts *config.CliOpts) error {
513438
localTarantool, err := util.JoinAbspath(cliOpts.Env.BinDir, "tarantool")

0 commit comments

Comments
 (0)