fix: list service child commands the service is called without arguments

This commit is contained in:
Florian Schade
2025-12-11 14:03:51 +01:00
parent 0fa3a2a7f8
commit 879de39129
3 changed files with 23 additions and 22 deletions

View File

@@ -22,18 +22,9 @@ func Execute() error {
Short: "opencloud",
})
for _, fn := range register.Commands {
cmd := fn(cfg)
// if the command has a RunE function, it is a subcommand of root
// if not it is a consumed root command from the services
// wee need to overwrite the RunE function to get the help output there
if cmd.RunE == nil {
cmd.RunE = func(cmd *cobra.Command, args []string) error {
cmd.Help()
return nil
}
}
app.AddCommand(cmd)
for _, commandFactory := range register.Commands {
command := commandFactory(cfg)
app.AddCommand(command)
}
app.SetArgs(os.Args[1:])
ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)

View File

@@ -52,7 +52,7 @@ import (
"github.com/spf13/cobra"
)
var svccmds = []register.Command{
var serviceCommands = []register.Command{
func(cfg *config.Config) *cobra.Command {
return ServiceCommand(cfg, cfg.Activitylog.Service.Name, activitylog.GetCommands(cfg.Activitylog), func(c *config.Config) {
cfg.Activitylog.Commons = cfg.Commons
@@ -265,9 +265,9 @@ var svccmds = []register.Command{
},
}
// ServiceCommand is the entry point for the all service commands.
func ServiceCommand(cfg *config.Config, serviceName string, subcommands []*cobra.Command, f func(*config.Config)) *cobra.Command {
svcCommand := &cobra.Command{
// ServiceCommand composes a cobra command from the given inputs.
func ServiceCommand(cfg *config.Config, serviceName string, subCommands []*cobra.Command, f func(*config.Config)) *cobra.Command {
command := &cobra.Command{
Use: serviceName,
Short: helper.SubcommandDescription(serviceName),
RunE: func(cmd *cobra.Command, args []string) error {
@@ -276,12 +276,21 @@ func ServiceCommand(cfg *config.Config, serviceName string, subcommands []*cobra
return nil
},
}
svcCommand.AddCommand(subcommands...)
return svcCommand
// if a service should have multiple child commands,
// we expect that at least one argument is needed;
// this helps cobra to force display all available child commands.
if len(subCommands) > 0 {
command.Args = cobra.MinimumNArgs(1)
}
command.AddCommand(subCommands...)
return command
}
func init() {
for _, c := range svccmds {
for _, c := range serviceCommands {
register.AddCommand(c)
}
}

View File

@@ -1,13 +1,14 @@
package register
import (
"github.com/opencloud-eu/opencloud/pkg/config"
"github.com/spf13/cobra"
"github.com/opencloud-eu/opencloud/pkg/config"
)
var (
// Commands defines the slice of commands.
Commands = []Command{}
// Commands define the slice of commands.
Commands []Command
)
// Command defines the register command.