Skip to content

Commit

Permalink
feat(server/v2): compartmentalize server components commands (#20743)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Jun 24, 2024
1 parent 1a7f859 commit ab1433b
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions server/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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...))
}
}

Expand Down

0 comments on commit ab1433b

Please sign in to comment.