|
1 | 1 | package cmd
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "strings" |
5 |
| - |
6 |
| - "github.com/apex/log" |
7 | 4 | "github.com/spf13/cobra"
|
8 | 5 | "github.com/tarantool/tt/cli/modules"
|
9 |
| - "github.com/tarantool/tt/cli/util" |
| 6 | + "golang.org/x/exp/slices" |
10 | 7 | )
|
11 | 8 |
|
12 | 9 | // configureExternalCmd configures external commands.
|
13 |
| -func configureExternalCmd(rootCmd *cobra.Command, |
14 |
| - modulesInfo *modules.ModulesInfo, forceInternal bool, args []string) { |
| 10 | +func configureExternalCmd(rootCmd *cobra.Command, modulesInfo *modules.ModulesInfo, |
| 11 | + forceInternal bool) { |
15 | 12 | configureExistsCmd(rootCmd, modulesInfo, forceInternal)
|
16 |
| - configureNonExistentCmd(rootCmd, modulesInfo, args) |
| 13 | + configureNonExistentCmd(rootCmd, modulesInfo) |
| 14 | +} |
| 15 | + |
| 16 | +// externalModuleHelpFunc returns function that displays help for the specified external module. |
| 17 | +func externalModuleHelpFunc(manifest modules.Manifest) func(*cobra.Command, []string) { |
| 18 | + return func(cmd *cobra.Command, args []string) { |
| 19 | + help, err := modules.GetExternalModuleHelp(manifest.Main) |
| 20 | + if err != nil { |
| 21 | + cmd.PrintErrf("failed to get help for module %q: %s\n", manifest.Name, err) |
| 22 | + return |
| 23 | + } |
| 24 | + cmd.Print(help) |
| 25 | + } |
17 | 26 | }
|
18 | 27 |
|
19 | 28 | // configureExistsCmd configures an external commands
|
20 | 29 | // that have internal implementation.
|
21 | 30 | func configureExistsCmd(rootCmd *cobra.Command, modulesInfo *modules.ModulesInfo,
|
22 | 31 | forceInternal bool) {
|
23 | 32 | for _, cmd := range rootCmd.Commands() {
|
24 |
| - if _, found := (*modulesInfo)[cmd.CommandPath()]; found { |
25 |
| - cmd.DisableFlagParsing = !forceInternal |
| 33 | + if manifest, found := (*modulesInfo)[cmd.CommandPath()]; found && !forceInternal { |
| 34 | + cmd.DisableFlagParsing = true |
| 35 | + cmd.SetHelpFunc(externalModuleHelpFunc(manifest)) |
26 | 36 | }
|
27 | 37 | }
|
28 | 38 | }
|
29 | 39 |
|
30 | 40 | // configureNonExistentCmd configures an external command that
|
31 | 41 | // 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 |
| - |
| 42 | +func configureNonExistentCmd(rootCmd *cobra.Command, modulesInfo *modules.ModulesInfo) { |
44 | 43 | // We avoid overwriting existing commands - we should add a command only
|
45 | 44 | // if it doesn't have an internal implementation in Tarantool CLI.
|
| 45 | + // So first collect list of internal command names. |
| 46 | + internalCmdNames := []string{"help"} |
46 | 47 | for _, cmd := range rootCmd.Commands() {
|
47 |
| - if cmd.Name() == externalCmd { |
48 |
| - return |
49 |
| - } |
| 48 | + internalCmdNames = append(internalCmdNames, cmd.Name()) |
50 | 49 | }
|
51 | 50 |
|
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"})) |
| 51 | + // Add external command only if it doesn't have an internal implementation in Tarantool CLI. |
| 52 | + for _, manifest := range *modulesInfo { |
| 53 | + if !slices.Contains(internalCmdNames, manifest.Name) { |
| 54 | + rootCmd.AddCommand(newExternalCmd(manifest)) |
| 55 | + } |
59 | 56 | }
|
60 | 57 | }
|
61 | 58 |
|
62 |
| -// newExternalCommand returns a pointer to a new external |
| 59 | +// newExternalCmd returns a pointer to a new external |
63 | 60 | // 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 |
| - }, |
| 61 | +func newExternalCmd(manifest modules.Manifest) *cobra.Command { |
| 62 | + var cmd = &cobra.Command{ |
| 63 | + Use: manifest.Name, |
| 64 | + Run: RunModuleFunc(nil), |
| 65 | + DisableFlagParsing: true, |
78 | 66 | }
|
79 |
| - |
80 |
| - cmd.DisableFlagParsing = true |
| 67 | + cmd.SetHelpFunc(externalModuleHelpFunc(manifest)) |
81 | 68 | return cmd
|
82 | 69 | }
|
0 commit comments