From 6d054c527c967bf2d0ee977a97d3928b1350e204 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Wed, 3 Dec 2025 15:06:00 +0100 Subject: [PATCH] migrate trash cli command from urfave/cli to spf13/cobra Signed-off-by: Christian Richter --- opencloud/pkg/command/trash.go | 63 ++++++++++++++++------------------ 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/opencloud/pkg/command/trash.go b/opencloud/pkg/command/trash.go index 9b26ca9068..0f28666bd1 100644 --- a/opencloud/pkg/command/trash.go +++ b/opencloud/pkg/command/trash.go @@ -3,57 +3,45 @@ package command import ( "fmt" - "github.com/opencloud-eu/opencloud/opencloud/pkg/trash" - "github.com/opencloud-eu/opencloud/opencloud/pkg/register" + "github.com/opencloud-eu/opencloud/opencloud/pkg/trash" "github.com/opencloud-eu/opencloud/pkg/config" "github.com/opencloud-eu/opencloud/pkg/config/configlog" "github.com/opencloud-eu/opencloud/pkg/config/parser" - "github.com/urfave/cli/v2" + + "github.com/spf13/cobra" ) -func TrashCommand(cfg *config.Config) *cli.Command { - return &cli.Command{ - Name: "trash", - Usage: "OpenCloud trash functionality", - Subcommands: []*cli.Command{ - TrashPurgeEmptyDirsCommand(cfg), - }, - Before: func(c *cli.Context) error { +func TrashCommand(cfg *config.Config) *cobra.Command { + trashCmd := &cobra.Command{ + Use: "trash", + Short: "OpenCloud trash functionality", + PreRunE: func(cmd *cobra.Command, args []string) error { return configlog.ReturnError(parser.ParseConfig(cfg, true)) }, - Action: func(_ *cli.Context) error { + RunE: func(cmd *cobra.Command, args []string) error { fmt.Println("Read the docs") return nil }, } + trashCmd.AddCommand(TrashPurgeEmptyDirsCommand(cfg)) + + return trashCmd } -func TrashPurgeEmptyDirsCommand(cfg *config.Config) *cli.Command { - return &cli.Command{ - Name: "purge-empty-dirs", - Usage: "purge empty directories", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "basepath", - Aliases: []string{"p"}, - Usage: "the basepath of the decomposedfs (e.g. /var/tmp/opencloud/storage/users)", - Required: true, - }, - &cli.BoolFlag{ - Name: "dry-run", - Usage: "do not delete anything, just print what would be deleted", - Value: true, - }, - }, - Action: func(c *cli.Context) error { - basePath := c.String("basepath") +func TrashPurgeEmptyDirsCommand(cfg *config.Config) *cobra.Command { + trashPurgeCmd := &cobra.Command{ + Use: "purge-empty-dirs", + Short: "purge empty directories", + RunE: func(cmd *cobra.Command, args []string) error { + basePath := cmd.Flag("basepath").Value.String() if basePath == "" { fmt.Println("basepath is required") - return cli.ShowCommandHelp(c, "trash") + _ = cmd.Help() + return nil } - if err := trash.PurgeTrashEmptyPaths(basePath, c.Bool("dry-run")); err != nil { + if err := trash.PurgeTrashEmptyPaths(basePath, cmd.Flag("dry-run").Changed); err != nil { fmt.Println(err) return err } @@ -61,6 +49,15 @@ func TrashPurgeEmptyDirsCommand(cfg *config.Config) *cli.Command { return nil }, } + trashPurgeCmd.Flags().StringP("basepath", "p", "", "the basepath of the decomposedfs (e.g. /var/tmp/opencloud/storage/users)") + err := trashPurgeCmd.MarkFlagRequired("basepath") + if err != nil { + fmt.Println(err) + } + + trashPurgeCmd.Flags().Bool("dry-run", true, "do not delete anything, just print what would be deleted") + + return trashPurgeCmd } func init() {