1
1
package cmd
2
2
3
3
import (
4
- "strings"
5
-
6
4
"github.com/spf13/cobra"
7
5
"github.com/tarantool/tt/cli/modules"
6
+ "golang.org/x/exp/slices"
7
+ )
8
+
9
+ var (
10
+ commandGroupExternal = & cobra.Group {ID : "External" }
8
11
)
9
12
10
13
// ExternalCmd configures external commands.
11
- func configureExternalCmd (rootCmd * cobra.Command ,
12
- modulesInfo * modules. ModulesInfo , forceInternal bool , args [] string ) {
14
+ func configureExternalCmd (rootCmd * cobra.Command , modulesInfo * modules. ModulesInfo ,
15
+ forceInternal bool ) {
13
16
configureExistsCmd (rootCmd , modulesInfo , forceInternal )
14
- configureNonExistentCmd (rootCmd , modulesInfo , args )
17
+ configureNonExistentCmd (rootCmd , modulesInfo )
15
18
}
16
19
17
20
// configureExistsCmd configures an external commands
@@ -21,35 +24,32 @@ func configureExistsCmd(rootCmd *cobra.Command, modulesInfo *modules.ModulesInfo
21
24
for _ , cmd := range rootCmd .Commands () {
22
25
if _ , found := (* modulesInfo )[cmd .CommandPath ()]; found {
23
26
cmd .DisableFlagParsing = ! forceInternal
27
+ cmd .GroupID = "|" + commandGroupExternal .ID
24
28
}
25
29
}
26
30
}
27
31
28
32
// configureNonExistentCmd configures an external command that
29
33
// has no internal implementation within the Tarantool CLI.
30
- func configureNonExistentCmd (rootCmd * cobra.Command ,
31
- modulesInfo * modules.ModulesInfo , args []string ) {
32
- // Since the user can pass flags, to determine the name of
33
- // an external command we have to take the first non-flag argument.
34
- externalCmd := args [0 ]
35
- for _ , name := range args {
36
- if ! strings .HasPrefix (name , "-" ) && name != "help" {
37
- externalCmd = name
38
- break
39
- }
40
- }
34
+ func configureNonExistentCmd (rootCmd * cobra.Command , modulesInfo * modules.ModulesInfo ) {
35
+ hasExternalCmd := false
41
36
42
- // We avoid overwriting existing commands - we should add a command only
43
- // if it doesn't have an internal implementation in Tarantool CLI.
37
+ // Prepare list of internal command names.
38
+ internalCmdNames := [] string { "help" }
44
39
for _ , cmd := range rootCmd .Commands () {
45
- if cmd .Name () == externalCmd {
46
- return
40
+ internalCmdNames = append (internalCmdNames , cmd .Name ())
41
+ }
42
+
43
+ // Add external command only if it doesn't have an internal implementation in Tarantool CLI.
44
+ for name , manifest := range * modulesInfo {
45
+ if ! slices .Contains (internalCmdNames , name ) {
46
+ rootCmd .AddCommand (newExternalCmd (name , manifest ))
47
+ hasExternalCmd = true
47
48
}
48
49
}
49
50
50
- externalCmdPath := rootCmd .Name () + " " + externalCmd
51
- if _ , found := (* modulesInfo )[externalCmdPath ]; found {
52
- rootCmd .AddCommand (newExternalCmd (externalCmd ))
51
+ if hasExternalCmd {
52
+ rootCmd .AddGroup (commandGroupExternal )
53
53
}
54
54
}
55
55
@@ -65,11 +65,18 @@ func externalCmdHelpFunc(cmd *cobra.Command, args []string) {
65
65
66
66
// newExternalCmd returns a pointer to a new external
67
67
// command that will call modules.RunCmd.
68
- func newExternalCmd (cmdName string ) * cobra.Command {
68
+ func newExternalCmd (cmdName string , manifest modules.Manifest ) * cobra.Command {
69
+ desc , err := modules .GetExternalModuleDescription (manifest )
70
+ if err != nil {
71
+ desc = "description is absent"
72
+ }
73
+
69
74
var cmd = & cobra.Command {
70
75
Use : cmdName ,
76
+ Short : desc ,
71
77
Run : RunModuleFunc (nil ),
72
78
DisableFlagParsing : true ,
79
+ GroupID : commandGroupExternal .ID ,
73
80
}
74
81
cmd .SetHelpFunc (externalCmdHelpFunc )
75
82
return cmd
0 commit comments