2016-11-27 19:36:13 +01:00
|
|
|
package rmdir
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2016-11-27 19:36:13 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2017-12-13 11:23:54 +01:00
|
|
|
var (
|
|
|
|
leaveRoot = false
|
|
|
|
)
|
|
|
|
|
2016-11-27 19:36:13 +01:00
|
|
|
func init() {
|
|
|
|
cmd.Root.AddCommand(rmdirsCmd)
|
2017-12-13 11:23:54 +01:00
|
|
|
rmdirsCmd.Flags().BoolVarP(&leaveRoot, "leave-root", "", leaveRoot, "Do not remove root directory if empty")
|
2016-11-27 19:36:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var rmdirsCmd = &cobra.Command{
|
|
|
|
Use: "rmdirs remote:path",
|
2017-07-21 19:17:57 +02:00
|
|
|
Short: `Remove empty directories under the path.`,
|
2020-11-13 11:07:34 +01:00
|
|
|
Long: `
|
|
|
|
This recursively removes any empty directories (including directories
|
|
|
|
that only contain empty directories), that it finds under the path.
|
|
|
|
The root path itself will also be removed if it is empty, unless
|
|
|
|
you supply the ` + "`--leave-root`" + ` flag.
|
2016-11-27 19:36:13 +01:00
|
|
|
|
2020-11-13 11:07:34 +01:00
|
|
|
Use command ` + "`rmdir`" + ` to delete just the empty directory
|
|
|
|
given by path, not recurse.
|
2017-12-13 11:23:54 +01:00
|
|
|
|
2016-11-27 19:36:13 +01:00
|
|
|
This is useful for tidying up remotes that rclone has left a lot of
|
2020-11-13 11:07:34 +01:00
|
|
|
empty directories in. For example the ` + "`delete`" + ` command will
|
|
|
|
delete files but leave the directory structure (unless used with
|
|
|
|
option ` + "`--rmdirs`" + `).
|
2016-11-27 19:36:13 +01:00
|
|
|
|
2020-11-13 11:07:34 +01:00
|
|
|
To delete a path and any objects in it, use ` + "`purge`" + ` command.
|
2016-11-27 19:36:13 +01:00
|
|
|
`,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
2018-05-07 18:58:16 +02:00
|
|
|
fdst := cmd.NewFsDir(args)
|
2016-12-04 17:52:24 +01:00
|
|
|
cmd.Run(true, false, command, func() error {
|
2019-06-17 10:34:30 +02:00
|
|
|
return operations.Rmdirs(context.Background(), fdst, "", leaveRoot)
|
2016-11-27 19:36:13 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|