Exemplo n.º 1
0
func TestRmdirs(t *testing.T) {
	r := NewRun(t)
	defer r.Finalise()
	r.Mkdir(r.fremote)

	// Clean any directories that have crept in so far
	// FIXME make the Finalise method do this?
	require.NoError(t, fs.Rmdirs(r.fremote))

	// Make some files and dirs we expect to keep
	r.ForceMkdir(r.fremote)
	file1 := r.WriteObject("A1/B1/C1/one", "aaa", t1)
	file2 := r.WriteObject("A1/two", "bbb", t2)
	//..and dirs we expect to delete
	require.NoError(t, fs.Mkdir(r.fremote, "A2"))
	require.NoError(t, fs.Mkdir(r.fremote, "A1/B2"))
	require.NoError(t, fs.Mkdir(r.fremote, "A1/B2/C2"))
	require.NoError(t, fs.Mkdir(r.fremote, "A1/B1/C3"))
	require.NoError(t, fs.Mkdir(r.fremote, "A3"))
	require.NoError(t, fs.Mkdir(r.fremote, "A3/B3"))
	require.NoError(t, fs.Mkdir(r.fremote, "A3/B3/C4"))

	fstest.CheckListingWithPrecision(
		t,
		r.fremote,
		[]fstest.Item{
			file1, file2,
		},
		[]string{
			"A1",
			"A1/B1",
			"A1/B1/C1",
			"A2",
			"A1/B2",
			"A1/B2/C2",
			"A1/B1/C3",
			"A3",
			"A3/B3",
			"A3/B3/C4",
		},
		fs.Config.ModifyWindow,
	)

	require.NoError(t, fs.Rmdirs(r.fremote))

	fstest.CheckListingWithPrecision(
		t,
		r.fremote,
		[]fstest.Item{
			file1, file2,
		},
		[]string{
			"A1",
			"A1/B1",
			"A1/B1/C1",
		},
		fs.Config.ModifyWindow,
	)

}
Exemplo n.º 2
0
Arquivo: rmdirs.go Projeto: ncw/rclone
import (
	"github.com/ncw/rclone/cmd"
	"github.com/ncw/rclone/fs"
	"github.com/spf13/cobra"
)

func init() {
	cmd.Root.AddCommand(rmdirsCmd)
}

var rmdirsCmd = &cobra.Command{
	Use:   "rmdirs remote:path",
	Short: `Remove any empty directoryies under the path.`,
	Long: `This removes any empty directories (or directories that only contain
empty directories) under the path that it finds, including the path if
it has nothing in.

This is useful for tidying up remotes that rclone has left a lot of
empty directories in.

`,
	Run: func(command *cobra.Command, args []string) {
		cmd.CheckArgs(1, 1, command, args)
		fdst := cmd.NewFsDst(args)
		cmd.Run(true, false, command, func() error {
			return fs.Rmdirs(fdst)
		})
	},
}