diff --git a/opencloud/pkg/command/shares.go b/opencloud/pkg/command/shares.go index e2bb37a0e..ccecacf85 100644 --- a/opencloud/pkg/command/shares.go +++ b/opencloud/pkg/command/shares.go @@ -2,14 +2,7 @@ package command import ( "errors" - - "github.com/rs/zerolog" - "github.com/urfave/cli/v2" - - "github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool" - "github.com/opencloud-eu/reva/v2/pkg/share/manager/jsoncs3" - "github.com/opencloud-eu/reva/v2/pkg/share/manager/registry" - "github.com/opencloud-eu/reva/v2/pkg/utils" + "fmt" "github.com/opencloud-eu/opencloud/opencloud/pkg/register" "github.com/opencloud-eu/opencloud/pkg/config" @@ -19,15 +12,22 @@ import ( mregistry "github.com/opencloud-eu/opencloud/pkg/registry" sharing "github.com/opencloud-eu/opencloud/services/sharing/pkg/config" sharingparser "github.com/opencloud-eu/opencloud/services/sharing/pkg/config/parser" + "github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool" + "github.com/opencloud-eu/reva/v2/pkg/share/manager/jsoncs3" + "github.com/opencloud-eu/reva/v2/pkg/share/manager/registry" + "github.com/opencloud-eu/reva/v2/pkg/utils" + "github.com/spf13/viper" + + "github.com/rs/zerolog" + "github.com/spf13/cobra" ) // SharesCommand is the entrypoint for the groups command. -func SharesCommand(cfg *config.Config) *cli.Command { - return &cli.Command{ - Name: "shares", - Usage: `cli tools to manage entries in the share manager.`, - Category: "maintenance", - Before: func(c *cli.Context) error { +func SharesCommand(cfg *config.Config) *cobra.Command { + sharesCmd := &cobra.Command{ + Use: "shares", + Short: `cli tools to manage entries in the share manager.`, + PreRunE: func(cmd *cobra.Command, args []string) error { // Parse base config if err := parser.ParseConfig(cfg, true); err != nil { return configlog.ReturnError(err) @@ -37,37 +37,21 @@ func SharesCommand(cfg *config.Config) *cli.Command { cfg.Sharing.Commons = cfg.Commons return configlog.ReturnError(sharingparser.ParseConfig(cfg.Sharing)) }, - Subcommands: []*cli.Command{ - cleanupCmd(cfg), - }, } + sharesCmd.AddCommand(cleanupCmd(cfg)) + + return sharesCmd } func init() { register.AddCommand(SharesCommand) } -func cleanupCmd(cfg *config.Config) *cli.Command { - return &cli.Command{ - Name: "cleanup", - Usage: `clean up stale entries in the share manager.`, - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "service-account-id", - Value: "", - Usage: "Name of the service account to use for the cleanup", - EnvVars: []string{"OC_SERVICE_ACCOUNT_ID"}, - Required: true, - }, - &cli.StringFlag{ - Name: "service-account-secret", - Value: "", - Usage: "Secret for the service account", - EnvVars: []string{"OC_SERVICE_ACCOUNT_SECRET"}, - Required: true, - }, - }, - Before: func(c *cli.Context) error { +func cleanupCmd(cfg *config.Config) *cobra.Command { + cleanCmd := &cobra.Command{ + Use: "cleanup", + Short: `clean up stale entries in the share manager.`, + PreRunE: func(cmd *cobra.Command, args []string) error { // Parse base config if err := parser.ParseConfig(cfg, true); err != nil { return configlog.ReturnError(err) @@ -77,13 +61,34 @@ func cleanupCmd(cfg *config.Config) *cli.Command { cfg.Sharing.Commons = cfg.Commons return configlog.ReturnError(sharingparser.ParseConfig(cfg.Sharing)) }, - Action: func(c *cli.Context) error { - return cleanup(c, cfg) + RunE: func(cmd *cobra.Command, args []string) error { + return cleanup(cmd, cfg) }, } + cleanCmd.Flags().String("service-account-id", "", "Name of the service account to use for the cleanup") + err := viper.BindEnv("service-account-id", "OC_SERVICE_ACCOUNT_ID") + if err != nil { + fmt.Printf("Could not bind environment variable OC_SERVICE_ACCOUNT_ID: %s", err) + } + err = viper.BindPFlag("service-account-id", cleanCmd.Flags().Lookup("service-account-id")) + if err != nil { + fmt.Printf("Could not bind flag OC_SERVICE_ACCOUNT_ID: %s", err) + } + + cleanCmd.Flags().String("service-account-secret", "", "Secret for the service account") + err = viper.BindEnv("service-account-secret", "OC_SERVICE_ACCOUNT_SECRET") + if err != nil { + fmt.Printf("Could not bind environment variable OC_SERVICE_ACCOUNT_SECRET: %s", err) + } + err = viper.BindPFlag("service-account-secret", cleanCmd.Flags().Lookup("service-account-secret")) + if err != nil { + fmt.Printf("Could not bind flag OC_SERVICE_ACCOUNT_SECRET: %s", err) + } + + return cleanCmd } -func cleanup(c *cli.Context, cfg *config.Config) error { +func cleanup(cmd *cobra.Command, cfg *config.Config) error { driver := cfg.Sharing.UserSharingDriver // cleanup is only implemented for the jsoncs3 share manager if driver != "jsoncs3" { @@ -114,7 +119,8 @@ func cleanup(c *cli.Context, cfg *config.Config) error { return configlog.ReturnError(err) } - serviceUserCtx, err := utils.GetServiceUserContext(c.String("service-account-id"), client, c.String("service-account-secret")) + serviceUserCtx, err := utils.GetServiceUserContext(cmd.Flag("service-account-id").Value.String(), + client, cmd.Flag("service-account-secret").Value.String()) if err != nil { return configlog.ReturnError(err) }