@@ -2,82 +2,52 @@ package cmd
2
2
3
3
import (
4
4
"fmt"
5
- "os"
6
5
"strings"
7
6
8
- "github.com/apex/log"
9
7
"github.com/spf13/cobra"
10
8
"github.com/tarantool/tt/cli/cmdcontext"
11
9
"github.com/tarantool/tt/cli/modules"
12
10
"github.com/tarantool/tt/cli/util"
13
11
)
14
12
15
- // DefaultHelpFunc is a type of the standard built-in
16
- // cobra implementation of the help function.
17
- type DefaultHelpFunc func (* cobra.Command , []string )
18
-
19
- // configureHelpCommand configures our own help module
20
- // so that it can be external as well.
21
- // If the help is called for an external module
22
- // (for example `tt help version`), we try to get the help from it.
23
- func configureHelpCommand (cmdCtx * cmdcontext.CmdCtx , rootCmd * cobra.Command ) error {
13
+ func configureHelpCommand (rootCmd * cobra.Command , modulesInfo * modules.ModulesInfo ) error {
24
14
// Add information about external modules into help template.
25
- rootCmd .SetUsageTemplate (fmt .Sprintf (usageTemplate , getExternalCommandsString (& modulesInfo )))
26
- defaultHelp := rootCmd . HelpFunc ()
27
-
28
- rootCmd . SetHelpFunc ( func ( cmd * cobra. Command , args [] string ) {
29
- cmdCtx . CommandName = cmd . Name ( )
30
- if len (os . Args ) == 1 || util . Find ( args , "-h" ) != - 1 || util . Find ( args , "--help" ) != - 1 {
31
- defaultHelp ( cmd , nil )
32
- return
15
+ rootCmd .SetUsageTemplate (fmt .Sprintf (usageTemplate , getExternalCommandsString (modulesInfo )))
16
+
17
+ internalHelpModule := func ( cmdCtx * cmdcontext. CmdCtx , args [] string ) error {
18
+ fmt . Printf ( "internalHelpModule: cmdCtx.CommandName=%s \n " , cmdCtx . CommandName )
19
+ fmt . Printf ( "internalHelpModule: args: %v \n " , args )
20
+ if len (args ) == 0 {
21
+ rootCmd . Help ( )
22
+ return nil
33
23
}
34
24
35
- args = modules . GetDefaultCmdArgs ( "help" )
36
- err := modules . RunCmd ( cmdCtx , "tt help " , & modulesInfo ,
37
- getInternalHelpFunc ( cmd , defaultHelp ), args )
25
+ cmd , _ , err := rootCmd . Find ( args )
26
+ fmt . Printf ( "internalHelpModule: cmd.Name=%s \n " , cmd . Name ())
27
+ fmt . Printf ( "internalHelpModule: cmd.CommandPath=%s \n " , cmd . CommandPath () )
38
28
if err != nil {
39
- log . Fatalf ( err . Error ())
29
+ return err
40
30
}
41
- })
31
+
32
+ cmd .Help ()
33
+ return nil
34
+ }
35
+
36
+ helpCmd := & cobra.Command {
37
+ Use : "help [command]" ,
38
+ Short : "Help about any command" ,
39
+ Run : RunModuleFunc (internalHelpModule ),
40
+ }
42
41
43
42
// Add valid arguments for completion.
44
- helpCmd := util .GetHelpCommand (rootCmd )
45
- for name := range modulesInfo {
43
+ for name := range * modulesInfo {
46
44
helpCmd .ValidArgs = append (helpCmd .ValidArgs , name )
47
45
}
48
46
47
+ rootCmd .SetHelpCommand (helpCmd )
49
48
return nil
50
49
}
51
50
52
- // getInternalHelpFunc returns a internal implementation of help module.
53
- func getInternalHelpFunc (cmd * cobra.Command , help DefaultHelpFunc ) modules.InternalFunc {
54
- return func (cmdCtx * cmdcontext.CmdCtx , args []string ) error {
55
- switch manifest , found := modulesInfo [cmd .CommandPath ()]; {
56
- // Cases when we have to run the "default" help:
57
- // - `tt help` and no external help module.
58
- // It looks strange: if we type the command `tt help`,
59
- // the call to cmd.Name() returns `tt` and I don’t know
60
- // what is the reason. If there is an external help module,
61
- // then we also cannot be here (see the code below) and it
62
- // is enough to check cmd.Name() == "tt".
63
- // - `tt help -I`
64
- // - `tt --help`, `tt -h` or `tt` (look code above).
65
- case cmd .Name () == "tt" , ! found :
66
- help (cmd , nil )
67
- // We make a call to the external module (if it exists) with the `--help` flag.
68
- default :
69
- helpMsg , err := modules .GetExternalModuleHelp (manifest .Main )
70
- if err != nil {
71
- return err
72
- }
73
-
74
- cmd .Print (helpMsg )
75
- }
76
-
77
- return nil
78
- }
79
- }
80
-
81
51
// getExternalCommandString returns a pretty string
82
52
// of descriptions for external modules.
83
53
func getExternalCommandsString (modulesInfo * modules.ModulesInfo ) string {
0 commit comments