From 8d6f7a8942a194f969aff214eef6072bb1e44e99 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Tue, 2 Dec 2025 14:42:38 +0100 Subject: [PATCH] migrate graph from urfave/cli to spf13/cobra Signed-off-by: Christian Richter --- services/graph/pkg/command/health.go | 16 ++++++------ services/graph/pkg/command/root.go | 21 ++++++++------- services/graph/pkg/command/server.go | 25 +++++++++--------- services/graph/pkg/command/unified_roles.go | 29 ++++++++++----------- services/graph/pkg/command/version.go | 16 ++++++------ 5 files changed, 53 insertions(+), 54 deletions(-) diff --git a/services/graph/pkg/command/health.go b/services/graph/pkg/command/health.go index 06c3dd58da..cac63221b8 100644 --- a/services/graph/pkg/command/health.go +++ b/services/graph/pkg/command/health.go @@ -8,19 +8,19 @@ import ( "github.com/opencloud-eu/opencloud/services/graph/pkg/config" "github.com/opencloud-eu/opencloud/services/graph/pkg/config/parser" "github.com/opencloud-eu/opencloud/services/graph/pkg/logging" - "github.com/urfave/cli/v2" + + "github.com/spf13/cobra" ) // Health is the entrypoint for the health command. -func Health(cfg *config.Config) *cli.Command { - return &cli.Command{ - Name: "health", - Usage: "check health status", - Category: "info", - Before: func(c *cli.Context) error { +func Health(cfg *config.Config) *cobra.Command { + return &cobra.Command{ + Use: "health", + Short: "check health status", + PreRunE: func(cmd *cobra.Command, args []string) error { return configlog.ReturnError(parser.ParseConfig(cfg)) }, - Action: func(c *cli.Context) error { + RunE: func(cmd *cobra.Command, args []string) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) resp, err := http.Get( diff --git a/services/graph/pkg/command/root.go b/services/graph/pkg/command/root.go index 6cd1a0b66b..e9f8c3f30e 100644 --- a/services/graph/pkg/command/root.go +++ b/services/graph/pkg/command/root.go @@ -4,15 +4,14 @@ import ( "os" "github.com/opencloud-eu/opencloud/pkg/clihelper" - - "github.com/urfave/cli/v2" - "github.com/opencloud-eu/opencloud/services/graph/pkg/config" + + "github.com/spf13/cobra" ) // GetCommands provides all commands for this service -func GetCommands(cfg *config.Config) cli.Commands { - return append([]*cli.Command{ +func GetCommands(cfg *config.Config) []*cobra.Command { + return append([]*cobra.Command{ // start this service Server(cfg), @@ -26,10 +25,12 @@ func GetCommands(cfg *config.Config) cli.Commands { // Execute is the entry point for the opencloud graph command. func Execute(cfg *config.Config) error { - app := clihelper.DefaultApp(&cli.App{ - Name: "graph", - Usage: "Serve Graph API for OpenCloud", - Commands: GetCommands(cfg), + app := clihelper.DefaultAppCobra(&cobra.Command{ + Use: "graph", + Short: "Serve Graph API for OpenCloud", }) - return app.RunContext(cfg.Context, os.Args) + app.AddCommand(GetCommands(cfg)...) + app.SetArgs(os.Args[1:]) + + return app.ExecuteContext(cfg.Context) } diff --git a/services/graph/pkg/command/server.go b/services/graph/pkg/command/server.go index 4642f88023..fb61fadd70 100644 --- a/services/graph/pkg/command/server.go +++ b/services/graph/pkg/command/server.go @@ -5,11 +5,6 @@ import ( "fmt" "os/signal" - "github.com/nats-io/nats.go" - "github.com/nats-io/nats.go/jetstream" - "github.com/pkg/errors" - "github.com/urfave/cli/v2" - "github.com/opencloud-eu/opencloud/pkg/config/configlog" "github.com/opencloud-eu/opencloud/pkg/runner" "github.com/opencloud-eu/opencloud/pkg/tracing" @@ -20,20 +15,24 @@ import ( "github.com/opencloud-eu/opencloud/services/graph/pkg/metrics" "github.com/opencloud-eu/opencloud/services/graph/pkg/server/debug" "github.com/opencloud-eu/opencloud/services/graph/pkg/server/http" + + "github.com/nats-io/nats.go" + "github.com/nats-io/nats.go/jetstream" + "github.com/pkg/errors" + "github.com/spf13/cobra" ) // Server is the entrypoint for the server command. -func Server(cfg *config.Config) *cli.Command { - return &cli.Command{ - Name: "server", - Usage: fmt.Sprintf("start the %s service without runtime (unsupervised mode)", cfg.Service.Name), - Category: "server", - Before: func(c *cli.Context) error { +func Server(cfg *config.Config) *cobra.Command { + return &cobra.Command{ + Use: "server", + Short: fmt.Sprintf("start the %s service without runtime (unsupervised mode)", cfg.Service.Name), + PreRunE: func(cmd *cobra.Command, args []string) error { return configlog.ReturnFatal(parser.ParseConfig(cfg)) }, - Action: func(c *cli.Context) error { + RunE: func(cmd *cobra.Command, args []string) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) - traceProvider, err := tracing.GetTraceProvider(c.Context, cfg.Commons.TracesExporter, cfg.Service.Name) + traceProvider, err := tracing.GetTraceProvider(cmd.Context(), cfg.Commons.TracesExporter, cfg.Service.Name) if err != nil { return err } diff --git a/services/graph/pkg/command/unified_roles.go b/services/graph/pkg/command/unified_roles.go index 6df9f9703a..531eacab12 100644 --- a/services/graph/pkg/command/unified_roles.go +++ b/services/graph/pkg/command/unified_roles.go @@ -5,15 +5,15 @@ import ( "slices" "strings" - "github.com/olekukonko/tablewriter" - "github.com/olekukonko/tablewriter/renderer" - "github.com/olekukonko/tablewriter/tw" - "github.com/urfave/cli/v2" - "github.com/opencloud-eu/opencloud/pkg/config/configlog" "github.com/opencloud-eu/opencloud/services/graph/pkg/config" "github.com/opencloud-eu/opencloud/services/graph/pkg/config/parser" "github.com/opencloud-eu/opencloud/services/graph/pkg/unifiedrole" + + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" + "github.com/olekukonko/tablewriter/tw" + "github.com/spf13/cobra" ) var ( @@ -34,15 +34,14 @@ var ( ) // UnifiedRoles bundles available commands for unified roles -func UnifiedRoles(cfg *config.Config) cli.Commands { - cmds := cli.Commands{ +func UnifiedRoles(cfg *config.Config) []*cobra.Command { + cmds := []*cobra.Command{ listUnifiedRoles(cfg), } for _, cmd := range cmds { - cmd.Category = "unified-roles" - cmd.Name = strings.Join([]string{cmd.Name, "unified-roles"}, "-") - cmd.Before = func(c *cli.Context) error { + cmd.Use = strings.Join([]string{cmd.Use, "unified-roles"}, "-") + cmd.PreRunE = func(cmd *cobra.Command, args []string) error { return configlog.ReturnError(parser.ParseConfig(cfg)) } } @@ -51,11 +50,11 @@ func UnifiedRoles(cfg *config.Config) cli.Commands { } // unifiedRolesStatus lists available unified roles, it contains an indicator to show if the role is enabled or not -func listUnifiedRoles(cfg *config.Config) *cli.Command { - return &cli.Command{ - Name: "list", - Usage: "list available unified roles", - Action: func(c *cli.Context) error { +func listUnifiedRoles(cfg *config.Config) *cobra.Command { + return &cobra.Command{ + Use: "list", + Short: "list available unified roles", + RunE: func(cmd *cobra.Command, args []string) error { r := tw.Rendition{ Settings: tw.Settings{ Separators: tw.Separators{ diff --git a/services/graph/pkg/command/version.go b/services/graph/pkg/command/version.go index ac418dc338..6836f10277 100644 --- a/services/graph/pkg/command/version.go +++ b/services/graph/pkg/command/version.go @@ -6,20 +6,20 @@ import ( "github.com/opencloud-eu/opencloud/pkg/registry" "github.com/opencloud-eu/opencloud/pkg/version" + "github.com/opencloud-eu/opencloud/services/graph/pkg/config" "github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter/tw" - "github.com/opencloud-eu/opencloud/services/graph/pkg/config" - "github.com/urfave/cli/v2" + + "github.com/spf13/cobra" ) // Version prints the service versions of all running instances. -func Version(cfg *config.Config) *cli.Command { - return &cli.Command{ - Name: "version", - Usage: "print the version of this binary and the running service instances", - Category: "info", - Action: func(c *cli.Context) error { +func Version(cfg *config.Config) *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "print the version of this binary and the running service instances", + RunE: func(cmd *cobra.Command, args []string) error { fmt.Println("Version: " + version.GetString()) fmt.Printf("Compiled: %s\n", version.Compiled()) fmt.Println("")