From 5080aeaeb9fe08582b04d4b42a5d904a92e9e725 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Tue, 2 Dec 2025 15:15:04 +0100 Subject: [PATCH] migrate notifications from urfave/cli to spf13/cobra Signed-off-by: Christian Richter --- services/notifications/pkg/command/health.go | 13 ++--- services/notifications/pkg/command/root.go | 18 +++---- .../notifications/pkg/command/send_email.go | 48 ++++++++++--------- services/notifications/pkg/command/server.go | 33 ++++++------- services/notifications/pkg/command/version.go | 14 +++--- 5 files changed, 65 insertions(+), 61 deletions(-) diff --git a/services/notifications/pkg/command/health.go b/services/notifications/pkg/command/health.go index 42a326e84..b2999ca1d 100644 --- a/services/notifications/pkg/command/health.go +++ b/services/notifications/pkg/command/health.go @@ -2,15 +2,16 @@ package command import ( "github.com/opencloud-eu/opencloud/services/notifications/pkg/config" - "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", - Action: func(c *cli.Context) error { +func Health(cfg *config.Config) *cobra.Command { + return &cobra.Command{ + Use: "health", + Short: "Check health status", + RunE: func(cmd *cobra.Command, args []string) error { // Not implemented return nil }, diff --git a/services/notifications/pkg/command/root.go b/services/notifications/pkg/command/root.go index 977796e61..dc632c6b5 100644 --- a/services/notifications/pkg/command/root.go +++ b/services/notifications/pkg/command/root.go @@ -5,12 +5,13 @@ import ( "github.com/opencloud-eu/opencloud/pkg/clihelper" "github.com/opencloud-eu/opencloud/services/notifications/pkg/config" - "github.com/urfave/cli/v2" + + "github.com/spf13/cobra" ) // GetCommands provides all commands for this service -func GetCommands(cfg *config.Config) cli.Commands { - return []*cli.Command{ +func GetCommands(cfg *config.Config) []*cobra.Command { + return []*cobra.Command{ // start this service Server(cfg), @@ -25,11 +26,12 @@ func GetCommands(cfg *config.Config) cli.Commands { // Execute is the entry point for the notifications command. func Execute(cfg *config.Config) error { - app := clihelper.DefaultApp(&cli.App{ - Name: "notifications", - Usage: "starts notifications service", - Commands: GetCommands(cfg), + app := clihelper.DefaultAppCobra(&cobra.Command{ + Use: "notifications", + Short: "starts notifications service", }) + app.AddCommand(GetCommands(cfg)...) + app.SetArgs(os.Args[1:]) - return app.RunContext(cfg.Context, os.Args) + return app.ExecuteContext(cfg.Context) } diff --git a/services/notifications/pkg/command/send_email.go b/services/notifications/pkg/command/send_email.go index c2582c3a0..a496c5ad7 100644 --- a/services/notifications/pkg/command/send_email.go +++ b/services/notifications/pkg/command/send_email.go @@ -5,30 +5,19 @@ import ( "github.com/opencloud-eu/opencloud/services/notifications/pkg/config" "github.com/opencloud-eu/reva/v2/pkg/events" "github.com/opencloud-eu/reva/v2/pkg/events/stream" + "github.com/pkg/errors" - "github.com/urfave/cli/v2" + "github.com/spf13/cobra" ) // SendEmail triggers the sending of grouped email notifications for daily or weekly emails. -func SendEmail(cfg *config.Config) *cli.Command { - return &cli.Command{ - Name: "send-email", - Usage: "Send grouped email notifications with daily or weekly interval. Specify at least one of the flags '--daily' or '--weekly'.", - Flags: []cli.Flag{ - &cli.BoolFlag{ - Name: "daily", - Aliases: []string{"d"}, - Usage: "Sends grouped daily email notifications.", - }, - &cli.BoolFlag{ - Name: "weekly", - Aliases: []string{"w"}, - Usage: "Sends grouped weekly email notifications.", - }, - }, - Action: func(c *cli.Context) error { - daily := c.Bool("daily") - weekly := c.Bool("weekly") +func SendEmail(cfg *config.Config) *cobra.Command { + sendEmailCmd := &cobra.Command{ + Use: "send-email", + Short: "Send grouped email notifications with daily or weekly interval. Specify at least one of the flags '--daily' or '--weekly'.", + RunE: func(cmd *cobra.Command, args []string) error { + daily := cmd.Flag("daily").Changed + weekly := cmd.Flag("weekly").Changed if !daily && !weekly { return errors.New("at least one of '--daily' or '--weekly' must be set") } @@ -38,7 +27,7 @@ func SendEmail(cfg *config.Config) *cli.Command { return err } if daily { - err = events.Publish(c.Context, s, events.SendEmailsEvent{ + err = events.Publish(cmd.Context(), s, events.SendEmailsEvent{ Interval: "daily", }) if err != nil { @@ -46,7 +35,7 @@ func SendEmail(cfg *config.Config) *cli.Command { } } if weekly { - err = events.Publish(c.Context, s, events.SendEmailsEvent{ + err = events.Publish(cmd.Context(), s, events.SendEmailsEvent{ Interval: "weekly", }) if err != nil { @@ -56,4 +45,19 @@ func SendEmail(cfg *config.Config) *cli.Command { return nil }, } + + sendEmailCmd.Flags().BoolP( + "daily", + "d", + false, + "Sends grouped daily email notifications.", + ) + + sendEmailCmd.Flags().BoolP( + "weekly", + "w", + false, + "Sends grouped weekly email notifications.", + ) + return sendEmailCmd } diff --git a/services/notifications/pkg/command/server.go b/services/notifications/pkg/command/server.go index 91ab0863f..7c2896abe 100644 --- a/services/notifications/pkg/command/server.go +++ b/services/notifications/pkg/command/server.go @@ -6,16 +6,6 @@ import ( "os/signal" "reflect" - ehsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/eventhistory/v0" - "github.com/opencloud-eu/reva/v2/pkg/store" - microstore "go-micro.dev/v4/store" - - "github.com/urfave/cli/v2" - - "github.com/opencloud-eu/reva/v2/pkg/events" - "github.com/opencloud-eu/reva/v2/pkg/events/stream" - "github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool" - "github.com/opencloud-eu/opencloud/pkg/config/configlog" "github.com/opencloud-eu/opencloud/pkg/generators" "github.com/opencloud-eu/opencloud/pkg/registry" @@ -29,21 +19,28 @@ import ( "github.com/opencloud-eu/opencloud/services/notifications/pkg/logging" "github.com/opencloud-eu/opencloud/services/notifications/pkg/server/debug" "github.com/opencloud-eu/opencloud/services/notifications/pkg/service" + "github.com/opencloud-eu/reva/v2/pkg/events" + "github.com/opencloud-eu/reva/v2/pkg/events/stream" + "github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool" + + ehsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/eventhistory/v0" + "github.com/opencloud-eu/reva/v2/pkg/store" + "github.com/spf13/cobra" + microstore "go-micro.dev/v4/store" ) // 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/notifications/pkg/command/version.go b/services/notifications/pkg/command/version.go index e02e9136e..e0595ea1b 100644 --- a/services/notifications/pkg/command/version.go +++ b/services/notifications/pkg/command/version.go @@ -2,16 +2,16 @@ package command import ( "github.com/opencloud-eu/opencloud/services/notifications/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 { // not implemented return nil },