diff --git a/opencloud/pkg/command/root.go b/opencloud/pkg/command/root.go index a0f42f4b5..1a80aa60f 100644 --- a/opencloud/pkg/command/root.go +++ b/opencloud/pkg/command/root.go @@ -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) diff --git a/opencloud/pkg/command/services.go b/opencloud/pkg/command/services.go index f6b05e843..de84b049f 100644 --- a/opencloud/pkg/command/services.go +++ b/opencloud/pkg/command/services.go @@ -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) } } diff --git a/opencloud/pkg/register/command.go b/opencloud/pkg/register/command.go index ac73c89a9..6a81f5047 100644 --- a/opencloud/pkg/register/command.go +++ b/opencloud/pkg/register/command.go @@ -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.