From c8d021729b238a024f9078010822a52962c12b19 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Tue, 2 Dec 2025 14:56:20 +0100 Subject: [PATCH] migrate idm from urfave/cli to spf13/cobra Signed-off-by: Christian Richter --- services/idm/pkg/command/health.go | 16 +++++------ services/idm/pkg/command/resetpw.go | 42 ++++++++++++++--------------- services/idm/pkg/command/root.go | 18 +++++++------ services/idm/pkg/command/server.go | 25 +++++++++-------- services/idm/pkg/command/version.go | 14 +++++----- 5 files changed, 58 insertions(+), 57 deletions(-) diff --git a/services/idm/pkg/command/health.go b/services/idm/pkg/command/health.go index 0283dc5f97..050dd35e24 100644 --- a/services/idm/pkg/command/health.go +++ b/services/idm/pkg/command/health.go @@ -8,19 +8,19 @@ import ( "github.com/opencloud-eu/opencloud/services/idm/pkg/config" "github.com/opencloud-eu/opencloud/services/idm/pkg/config/parser" "github.com/opencloud-eu/opencloud/services/idm/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/idm/pkg/command/resetpw.go b/services/idm/pkg/command/resetpw.go index 8674fbb664..ab9d5f0c9c 100644 --- a/services/idm/pkg/command/resetpw.go +++ b/services/idm/pkg/command/resetpw.go @@ -8,44 +8,44 @@ import ( "syscall" "time" - "github.com/go-ldap/ldap/v3" - "github.com/libregraph/idm/pkg/ldbbolt" - "github.com/libregraph/idm/server" "github.com/opencloud-eu/opencloud/pkg/config/configlog" "github.com/opencloud-eu/opencloud/pkg/log" "github.com/opencloud-eu/opencloud/services/idm/pkg/config" "github.com/opencloud-eu/opencloud/services/idm/pkg/config/parser" "github.com/opencloud-eu/opencloud/services/idm/pkg/logging" - "github.com/urfave/cli/v2" + + "github.com/go-ldap/ldap/v3" + "github.com/libregraph/idm/pkg/ldbbolt" + "github.com/libregraph/idm/server" + "github.com/spf13/cobra" bolt "go.etcd.io/bbolt" "golang.org/x/term" ) // ResetPassword is the entrypoint for the resetpassword command -func ResetPassword(cfg *config.Config) *cli.Command { - return &cli.Command{ - Name: "resetpassword", - Usage: "Reset user password", - Category: "password reset", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "user-name", - Aliases: []string{"u"}, - Usage: "User name", - Value: "admin", - }, - }, - Before: func(_ *cli.Context) error { +func ResetPassword(cfg *config.Config) *cobra.Command { + resetPasswordCmd := &cobra.Command{ + Use: "resetpassword", + Short: "Reset user password", + 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) - ctx, cancel := context.WithCancel(c.Context) + ctx, cancel := context.WithCancel(cmd.Context()) defer cancel() - return resetPassword(ctx, logger, cfg, c.String("user-name")) + return resetPassword(ctx, logger, cfg, cmd.Flag("user-name").Value.String()) }, } + resetPasswordCmd.Flags().StringP( + "user-name", + "u", + "admin", + "User name", + ) + + return resetPasswordCmd } func resetPassword(_ context.Context, logger log.Logger, cfg *config.Config, userName string) error { diff --git a/services/idm/pkg/command/root.go b/services/idm/pkg/command/root.go index 8a84bf814a..c0fb60ba96 100644 --- a/services/idm/pkg/command/root.go +++ b/services/idm/pkg/command/root.go @@ -5,12 +5,13 @@ import ( "github.com/opencloud-eu/opencloud/pkg/clihelper" "github.com/opencloud-eu/opencloud/services/idm/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 opencloud idm command. func Execute(cfg *config.Config) error { - app := clihelper.DefaultApp(&cli.App{ - Name: "idm", - Usage: "Embedded LDAP service for OpenCloud", - Commands: GetCommands(cfg), + app := clihelper.DefaultAppCobra(&cobra.Command{ + Use: "idm", + Short: "Embedded LDAP service for OpenCloud", }) + 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/idm/pkg/command/server.go b/services/idm/pkg/command/server.go index 3d860381a6..a71991563e 100644 --- a/services/idm/pkg/command/server.go +++ b/services/idm/pkg/command/server.go @@ -10,12 +10,6 @@ import ( "os/signal" "strings" - "github.com/go-ldap/ldif" - "github.com/libregraph/idm/pkg/ldappassword" - "github.com/libregraph/idm/pkg/ldbbolt" - "github.com/libregraph/idm/server" - "github.com/urfave/cli/v2" - "github.com/opencloud-eu/opencloud/pkg/config/configlog" pkgcrypto "github.com/opencloud-eu/opencloud/pkg/crypto" "github.com/opencloud-eu/opencloud/pkg/log" @@ -25,18 +19,23 @@ import ( "github.com/opencloud-eu/opencloud/services/idm/pkg/config/parser" "github.com/opencloud-eu/opencloud/services/idm/pkg/logging" "github.com/opencloud-eu/opencloud/services/idm/pkg/server/debug" + + "github.com/go-ldap/ldif" + "github.com/libregraph/idm/pkg/ldappassword" + "github.com/libregraph/idm/pkg/ldbbolt" + "github.com/libregraph/idm/server" + "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 { var cancel context.CancelFunc if cfg.Context == nil { cfg.Context, cancel = signal.NotifyContext(context.Background(), runner.StopSignals...) diff --git a/services/idm/pkg/command/version.go b/services/idm/pkg/command/version.go index 859e5f20af..fab8d43d03 100644 --- a/services/idm/pkg/command/version.go +++ b/services/idm/pkg/command/version.go @@ -2,16 +2,16 @@ package command import ( "github.com/opencloud-eu/opencloud/services/idm/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 },