feat(server/v2): compartmentalize server components commands (#20743)

This commit is contained in:
Julien Robert
2024-06-24 09:17:02 +00:00
committed by GitHub
parent 1a7f859a72
commit ab1433bf16
+19 -3
View File
@@ -5,8 +5,10 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/pelletier/go-toml/v2"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
@@ -119,12 +121,26 @@ func (s *Server) Stop(ctx context.Context) error {
// CLICommands returns all CLI commands of all components.
func (s *Server) CLICommands() CLIConfig {
compart := func(name string, cmds ...*cobra.Command) *cobra.Command {
if len(cmds) == 1 && strings.HasPrefix(cmds[0].Use, name) {
return cmds[0]
}
rootCmd := &cobra.Command{
Use: name,
Short: fmt.Sprintf("Commands from the %s server component", name),
}
rootCmd.AddCommand(cmds...)
return rootCmd
}
commands := CLIConfig{}
for _, mod := range s.components {
if climod, ok := mod.(HasCLICommands); ok {
commands.Commands = append(commands.Commands, climod.CLICommands().Commands...)
commands.Queries = append(commands.Queries, climod.CLICommands().Queries...)
commands.Txs = append(commands.Txs, climod.CLICommands().Txs...)
commands.Commands = append(commands.Commands, compart(mod.Name(), climod.CLICommands().Commands...))
commands.Txs = append(commands.Txs, compart(mod.Name(), climod.CLICommands().Txs...))
commands.Queries = append(commands.Queries, compart(mod.Name(), climod.CLICommands().Queries...))
}
}