11package cmd
22
33import (
4- "strings"
5-
64 "github.com/spf13/cobra"
75 "github.com/tarantool/tt/cli/modules"
6+ "golang.org/x/exp/slices"
7+ )
8+
9+ var (
10+ commandGroupExternal = & cobra.Group {ID : "External" }
811)
912
1013// 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 ) {
1316 configureExistsCmd (rootCmd , modulesInfo , forceInternal )
14- configureNonExistentCmd (rootCmd , modulesInfo , args )
17+ configureNonExistentCmd (rootCmd , modulesInfo )
1518}
1619
1720// configureExistsCmd configures an external commands
@@ -21,35 +24,32 @@ func configureExistsCmd(rootCmd *cobra.Command, modulesInfo *modules.ModulesInfo
2124 for _ , cmd := range rootCmd .Commands () {
2225 if _ , found := (* modulesInfo )[cmd .CommandPath ()]; found {
2326 cmd .DisableFlagParsing = ! forceInternal
27+ cmd .GroupID = "|" + commandGroupExternal .ID
2428 }
2529 }
2630}
2731
2832// configureNonExistentCmd configures an external command that
2933// 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
4136
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" }
4439 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
4748 }
4849 }
4950
50- externalCmdPath := rootCmd .Name () + " " + externalCmd
51- if _ , found := (* modulesInfo )[externalCmdPath ]; found {
52- rootCmd .AddCommand (newExternalCmd (externalCmd ))
51+ if hasExternalCmd {
52+ rootCmd .AddGroup (commandGroupExternal )
5353 }
5454}
5555
@@ -65,11 +65,18 @@ func externalCmdHelpFunc(cmd *cobra.Command, args []string) {
6565
6666// newExternalCmd returns a pointer to a new external
6767// 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+
6974 var cmd = & cobra.Command {
7075 Use : cmdName ,
76+ Short : desc ,
7177 Run : RunModuleFunc (nil ),
7278 DisableFlagParsing : true ,
79+ GroupID : commandGroupExternal .ID ,
7380 }
7481 cmd .SetHelpFunc (externalCmdHelpFunc )
7582 return cmd
0 commit comments