@@ -2,82 +2,52 @@ package cmd
22
33import (
44 "fmt"
5- "os"
65 "strings"
76
8- "github.com/apex/log"
97 "github.com/spf13/cobra"
108 "github.com/tarantool/tt/cli/cmdcontext"
119 "github.com/tarantool/tt/cli/modules"
1210 "github.com/tarantool/tt/cli/util"
1311)
1412
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 {
2414 // 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
3323 }
3424
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 () )
3828 if err != nil {
39- log . Fatalf ( err . Error ())
29+ return err
4030 }
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+ }
4241
4342 // Add valid arguments for completion.
44- helpCmd := util .GetHelpCommand (rootCmd )
45- for name := range modulesInfo {
43+ for name := range * modulesInfo {
4644 helpCmd .ValidArgs = append (helpCmd .ValidArgs , name )
4745 }
4846
47+ rootCmd .SetHelpCommand (helpCmd )
4948 return nil
5049}
5150
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-
8151// getExternalCommandString returns a pretty string
8252// of descriptions for external modules.
8353func getExternalCommandsString (modulesInfo * modules.ModulesInfo ) string {
0 commit comments