delete: added --rmdirs flag to delete directories as well - fixes #4055

If you supply the --rmdirs flag with delete command,
it will remove all empty directories along with it
leaving the root intact.
This commit is contained in:
Kush 2020-04-29 16:45:30 +05:30 committed by GitHub
parent 1c8eab81a5
commit f555873f18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,12 +4,19 @@ import (
"context" "context"
"github.com/rclone/rclone/cmd" "github.com/rclone/rclone/cmd"
"github.com/rclone/rclone/fs/config/flags"
"github.com/rclone/rclone/fs/operations" "github.com/rclone/rclone/fs/operations"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
rmdirs = false
)
func init() { func init() {
cmd.Root.AddCommand(commandDefinition) cmd.Root.AddCommand(commandDefinition)
cmdFlags := commandDefinition.Flags()
flags.BoolVarP(cmdFlags, &rmdirs, "rmdirs", "", rmdirs, "rmdirs removes empty directories but leaves root intact")
} }
var commandDefinition = &cobra.Command{ var commandDefinition = &cobra.Command{
@ -23,6 +30,8 @@ filters so can be used to selectively delete files.
alone. If you want to delete a directory and all of its contents use alone. If you want to delete a directory and all of its contents use
` + "`" + `rclone purge` + "`" + ` ` + "`" + `rclone purge` + "`" + `
If you supply the --rmdirs flag, it will remove all empty directories along with it.
Eg delete all files bigger than 100MBytes Eg delete all files bigger than 100MBytes
Check what would be deleted first (use either) Check what would be deleted first (use either)
@ -41,7 +50,14 @@ delete all files bigger than 100MBytes.
cmd.CheckArgs(1, 1, command, args) cmd.CheckArgs(1, 1, command, args)
fsrc := cmd.NewFsSrc(args) fsrc := cmd.NewFsSrc(args)
cmd.Run(true, false, command, func() error { cmd.Run(true, false, command, func() error {
return operations.Delete(context.Background(), fsrc) if err := operations.Delete(context.Background(), fsrc); err != nil {
return err
}
if rmdirs {
fdst := cmd.NewFsDir(args)
return operations.Rmdirs(context.Background(), fdst, "", true)
}
return nil
}) })
}, },
} }