From 9904e5b932a673d2f6d39602e7a9b7dfe41e5579 Mon Sep 17 00:00:00 2001 From: Florian Schade Date: Thu, 11 Dec 2025 14:40:46 +0100 Subject: [PATCH] enhancement(cli): group commands by topic --- opencloud/pkg/command/command.go | 7 +++++++ opencloud/pkg/command/decomposedfs.go | 5 +++-- opencloud/pkg/command/helper/common.go | 9 --------- opencloud/pkg/command/init.go | 7 ++++--- opencloud/pkg/command/posixfs.go | 5 +++-- opencloud/pkg/command/root.go | 8 ++++++++ opencloud/pkg/command/server.go | 1 + opencloud/pkg/command/services.go | 8 +++++--- opencloud/pkg/command/version.go | 8 +++++--- 9 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 opencloud/pkg/command/command.go delete mode 100644 opencloud/pkg/command/helper/common.go diff --git a/opencloud/pkg/command/command.go b/opencloud/pkg/command/command.go new file mode 100644 index 000000000..964ec377a --- /dev/null +++ b/opencloud/pkg/command/command.go @@ -0,0 +1,7 @@ +package command + +const ( + CommandGroupServer = "Server" + CommandGroupServices = "Service" + CommandGroupStorage = "Storage" +) diff --git a/opencloud/pkg/command/decomposedfs.go b/opencloud/pkg/command/decomposedfs.go index 2f96b3ec5..f66ab47f5 100644 --- a/opencloud/pkg/command/decomposedfs.go +++ b/opencloud/pkg/command/decomposedfs.go @@ -33,8 +33,9 @@ import ( // DecomposedfsCommand is the entrypoint for the groups command. func DecomposedfsCommand(cfg *config.Config) *cobra.Command { decomposedCmd := &cobra.Command{ - Use: "decomposedfs", - Short: `cli tools to inspect and manipulate a decomposedfs storage.`, + Use: "decomposedfs", + Short: `cli tools to inspect and manipulate a decomposedfs storage.`, + GroupID: CommandGroupStorage, } decomposedCmd.AddCommand(metadataCmd(cfg), checkCmd(cfg)) return decomposedCmd diff --git a/opencloud/pkg/command/helper/common.go b/opencloud/pkg/command/helper/common.go deleted file mode 100644 index 1c2fdd990..000000000 --- a/opencloud/pkg/command/helper/common.go +++ /dev/null @@ -1,9 +0,0 @@ -package helper - -import ( - "fmt" -) - -func SubcommandDescription(serviceName string) string { - return fmt.Sprintf("%s service commands", serviceName) -} diff --git a/opencloud/pkg/command/init.go b/opencloud/pkg/command/init.go index d381dd993..84f37c674 100644 --- a/opencloud/pkg/command/init.go +++ b/opencloud/pkg/command/init.go @@ -17,10 +17,11 @@ import ( ) // InitCommand is the entrypoint for the init command -func InitCommand(cfg *config.Config) *cobra.Command { +func InitCommand(_ *config.Config) *cobra.Command { initCmd := &cobra.Command{ - Use: "init", - Short: "initialise an OpenCloud config", + Use: "init", + Short: "initialise an OpenCloud config", + GroupID: CommandGroupServer, RunE: func(cmd *cobra.Command, args []string) error { insecureFlag := cmd.Flag("insecure").Value.String() insecure := false diff --git a/opencloud/pkg/command/posixfs.go b/opencloud/pkg/command/posixfs.go index 88a173d74..070eb2b5d 100644 --- a/opencloud/pkg/command/posixfs.go +++ b/opencloud/pkg/command/posixfs.go @@ -40,8 +40,9 @@ type EntryInfo struct { // PosixfsCommand is the entrypoint for the posixfs command. func PosixfsCommand(cfg *config.Config) *cobra.Command { posixCmd := &cobra.Command{ - Use: "posixfs", - Short: `cli tools to inspect and manipulate a posixfs storage.`, + Use: "posixfs", + Short: `cli tools to inspect and manipulate a posixfs storage.`, + GroupID: CommandGroupStorage, } posixCmd.AddCommand(consistencyCmd(cfg)) diff --git a/opencloud/pkg/command/root.go b/opencloud/pkg/command/root.go index 1a80aa60f..834bc68e0 100644 --- a/opencloud/pkg/command/root.go +++ b/opencloud/pkg/command/root.go @@ -24,6 +24,14 @@ func Execute() error { for _, commandFactory := range register.Commands { command := commandFactory(cfg) + + if command.GroupID != "" && !app.ContainsGroup(command.GroupID) { + app.AddGroup(&cobra.Group{ + ID: command.GroupID, + Title: command.GroupID, + }) + } + app.AddCommand(command) } app.SetArgs(os.Args[1:]) diff --git a/opencloud/pkg/command/server.go b/opencloud/pkg/command/server.go index 68f6aa018..776f24d27 100644 --- a/opencloud/pkg/command/server.go +++ b/opencloud/pkg/command/server.go @@ -18,6 +18,7 @@ func Server(cfg *config.Config) *cobra.Command { PreRunE: func(cmd *cobra.Command, args []string) error { return configlog.ReturnError(parser.ParseConfig(cfg, false)) }, + GroupID: CommandGroupServer, RunE: func(cmd *cobra.Command, args []string) error { // Prefer the in-memory registry as the default when running in single-binary mode r := runtime.New(cfg) diff --git a/opencloud/pkg/command/services.go b/opencloud/pkg/command/services.go index de84b049f..a3cfd02be 100644 --- a/opencloud/pkg/command/services.go +++ b/opencloud/pkg/command/services.go @@ -1,7 +1,8 @@ package command import ( - "github.com/opencloud-eu/opencloud/opencloud/pkg/command/helper" + "fmt" + "github.com/opencloud-eu/opencloud/opencloud/pkg/register" "github.com/opencloud-eu/opencloud/pkg/config" "github.com/opencloud-eu/opencloud/pkg/config/configlog" @@ -268,8 +269,9 @@ var serviceCommands = []register.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), + Use: serviceName, + Short: fmt.Sprintf("%s service commands", serviceName), + GroupID: CommandGroupServices, RunE: func(cmd *cobra.Command, args []string) error { configlog.Error(parser.ParseConfig(cfg, true)) f(cfg) diff --git a/opencloud/pkg/command/version.go b/opencloud/pkg/command/version.go index 39fd6cc74..5f7791156 100644 --- a/opencloud/pkg/command/version.go +++ b/opencloud/pkg/command/version.go @@ -4,11 +4,12 @@ import ( "fmt" "os" + "github.com/spf13/cobra" + "github.com/opencloud-eu/opencloud/opencloud/pkg/register" "github.com/opencloud-eu/opencloud/pkg/config" "github.com/opencloud-eu/opencloud/pkg/registry" "github.com/opencloud-eu/opencloud/pkg/version" - "github.com/spf13/cobra" "github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter/tw" @@ -22,8 +23,9 @@ const ( // VersionCommand is the entrypoint for the version command. func VersionCommand(cfg *config.Config) *cobra.Command { versionCmd := &cobra.Command{ - Use: "version", - Short: "print the version of this binary and all running service instances", + Use: "version", + Short: "print the version of this binary and all running service instances", + GroupID: CommandGroupServer, RunE: func(cmd *cobra.Command, args []string) error { fmt.Println("Version: " + version.GetString()) fmt.Printf("Edition: %s\n", version.Edition)