From 78168a3a21571d3f6688bc9297cd7bd2fe513af9 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Tue, 2 Dec 2025 12:59:26 +0100 Subject: [PATCH] migrate auth-app from urfave/cli to spf13/cobra Signed-off-by: Christian Richter --- services/auth-app/pkg/command/create.go | 56 ++++++++++++------------ services/auth-app/pkg/command/health.go | 16 +++---- services/auth-app/pkg/command/root.go | 18 ++++---- services/auth-app/pkg/command/server.go | 18 ++++---- services/auth-app/pkg/command/version.go | 15 +++---- 5 files changed, 62 insertions(+), 61 deletions(-) diff --git a/services/auth-app/pkg/command/create.go b/services/auth-app/pkg/command/create.go index a037e04aa1..0d81535ab0 100644 --- a/services/auth-app/pkg/command/create.go +++ b/services/auth-app/pkg/command/create.go @@ -6,14 +6,10 @@ import ( authpb "github.com/cs3org/go-cs3apis/cs3/auth/provider/v1beta1" "github.com/opencloud-eu/reva/v2/pkg/auth/scope" + "github.com/spf13/cobra" "time" - applicationsv1beta1 "github.com/cs3org/go-cs3apis/cs3/auth/applications/v1beta1" - gatewayv1beta1 "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" - userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" - rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" - typesv1beta1 "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" "github.com/opencloud-eu/opencloud/pkg/config/configlog" "github.com/opencloud-eu/opencloud/pkg/registry" "github.com/opencloud-eu/opencloud/pkg/tracing" @@ -21,33 +17,25 @@ import ( "github.com/opencloud-eu/opencloud/services/auth-app/pkg/config/parser" ctxpkg "github.com/opencloud-eu/reva/v2/pkg/ctx" "github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool" - "github.com/urfave/cli/v2" + + applicationsv1beta1 "github.com/cs3org/go-cs3apis/cs3/auth/applications/v1beta1" + gatewayv1beta1 "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" + userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" + rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" + typesv1beta1 "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" "google.golang.org/grpc/metadata" ) // Create is the entrypoint for the app auth create command -func Create(cfg *config.Config) *cli.Command { - return &cli.Command{ - Name: "create", - Usage: "create an app auth token for a user", - Category: "maintenance", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "user-name", - Value: "", - Usage: "user to create the app-token for", - }, - &cli.StringFlag{ - Name: "expiration", - Value: "72h", - Usage: "expiration of the app password, e.g. 72h, 1h, 1m, 1s. Default is 72h.", - }, - }, - Before: func(_ *cli.Context) error { +func Create(cfg *config.Config) *cobra.Command { + createCmd := &cobra.Command{ + Use: "create", + Short: "create an app auth token for a user", + PreRunE: func(cmd *cobra.Command, args []string) error { return configlog.ReturnError(parser.ParseConfig(cfg)) }, - Action: func(c *cli.Context) error { - traceProvider, err := tracing.GetTraceProvider(c.Context, cfg.Commons.TracesExporter, cfg.Service.Name) + RunE: func(cmd *cobra.Command, args []string) error { + traceProvider, err := tracing.GetTraceProvider(cmd.Context(), cfg.Commons.TracesExporter, cfg.Service.Name) if err != nil { return err } @@ -68,7 +56,7 @@ func Create(cfg *config.Config) *cli.Command { return err } - userName := c.String("user-name") + userName := cmd.Flag("user-name").Value.String() if userName == "" { fmt.Printf("Username to create app token for: ") if _, err := fmt.Scanln(&userName); err != nil { @@ -97,7 +85,7 @@ func Create(cfg *config.Config) *cli.Command { return err } - expiry, err := time.ParseDuration(c.String("expiration")) + expiry, err := time.ParseDuration(cmd.Flag("expiration").Value.String()) if err != nil { return err } @@ -121,4 +109,16 @@ func Create(cfg *config.Config) *cli.Command { return nil }, } + createCmd.Flags().String( + "user-name", + "", + "user to create the app-token for", + ) + createCmd.Flags().String( + "expiration", + "72h", + "expiration of the app password, e.g. 72h, 1h, 1m, 1s. Default is 72h.", + ) + + return createCmd } diff --git a/services/auth-app/pkg/command/health.go b/services/auth-app/pkg/command/health.go index 627038b59a..fac1ec71a6 100644 --- a/services/auth-app/pkg/command/health.go +++ b/services/auth-app/pkg/command/health.go @@ -8,19 +8,19 @@ import ( "github.com/opencloud-eu/opencloud/services/auth-app/pkg/config" "github.com/opencloud-eu/opencloud/services/auth-app/pkg/config/parser" "github.com/opencloud-eu/opencloud/services/auth-app/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(_ *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(_ *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/auth-app/pkg/command/root.go b/services/auth-app/pkg/command/root.go index 3371c99df4..f05588e095 100644 --- a/services/auth-app/pkg/command/root.go +++ b/services/auth-app/pkg/command/root.go @@ -5,12 +5,13 @@ import ( "github.com/opencloud-eu/opencloud/pkg/clihelper" "github.com/opencloud-eu/opencloud/services/auth-app/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 auth-app command. func Execute(cfg *config.Config) error { - app := clihelper.DefaultApp(&cli.App{ - Name: "auth-app", - Usage: "Provide app authentication for OpenCloud", - Commands: GetCommands(cfg), + app := clihelper.DefaultAppCobra(&cobra.Command{ + Use: "auth-app", + Short: "Provide app authentication 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/auth-app/pkg/command/server.go b/services/auth-app/pkg/command/server.go index 2c3bb774f6..276bef46dd 100644 --- a/services/auth-app/pkg/command/server.go +++ b/services/auth-app/pkg/command/server.go @@ -20,25 +20,25 @@ import ( "github.com/opencloud-eu/opencloud/services/auth-app/pkg/server/http" "github.com/opencloud-eu/reva/v2/cmd/revad/runtime" "github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool" - "github.com/urfave/cli/v2" + + "github.com/spf13/cobra" ) // Server is the entry point 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(_ *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 { if cfg.AllowImpersonation { fmt.Println("WARNING: Impersonation is enabled. Admins can impersonate all users.") } 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/auth-app/pkg/command/version.go b/services/auth-app/pkg/command/version.go index 42b2c757ab..c6df8f5892 100644 --- a/services/auth-app/pkg/command/version.go +++ b/services/auth-app/pkg/command/version.go @@ -6,20 +6,19 @@ import ( "github.com/opencloud-eu/opencloud/pkg/registry" "github.com/opencloud-eu/opencloud/pkg/version" + "github.com/opencloud-eu/opencloud/services/auth-app/pkg/config" "github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter/tw" - "github.com/opencloud-eu/opencloud/services/auth-app/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("")