2018-09-29 21:48:29 +08:00
|
|
|
// Clean the left over test files
|
|
|
|
|
2018-11-24 00:05:09 +08:00
|
|
|
// +build go1.11
|
2018-10-25 04:33:42 +08:00
|
|
|
|
2018-09-29 21:48:29 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2018-09-29 21:48:29 +08:00
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
|
2019-02-07 19:29:52 +08:00
|
|
|
"github.com/pkg/errors"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/list"
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2018-09-29 21:48:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// MatchTestRemote matches the remote names used for testing (copied
|
|
|
|
// from fstest/fstest.go so we don't have to import that and get all
|
|
|
|
// its flags)
|
2019-02-07 19:29:52 +08:00
|
|
|
var MatchTestRemote = regexp.MustCompile(`^rclone-test-[abcdefghijklmnopqrstuvwxyz0123456789]{24}(_segments)?$`)
|
2018-09-29 21:48:29 +08:00
|
|
|
|
|
|
|
// cleanFs runs a single clean fs for left over directories
|
|
|
|
func cleanFs(remote string) error {
|
|
|
|
f, err := fs.NewFs(remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
entries, err := list.DirSorted(context.Background(), f, true, "")
|
2018-09-29 21:48:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-07 19:29:52 +08:00
|
|
|
var lastErr error
|
|
|
|
err = entries.ForDirError(func(dir fs.Directory) error {
|
2018-09-29 21:48:29 +08:00
|
|
|
dirPath := dir.Remote()
|
|
|
|
fullPath := remote + dirPath
|
|
|
|
if MatchTestRemote.MatchString(dirPath) {
|
|
|
|
if *dryRun {
|
|
|
|
log.Printf("Not Purging %s - -dry-run", fullPath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Printf("Purging %s", fullPath)
|
|
|
|
dir, err := fs.NewFs(fullPath)
|
|
|
|
if err != nil {
|
2019-02-07 19:29:52 +08:00
|
|
|
err = errors.Wrap(err, "NewFs failed")
|
|
|
|
lastErr = err
|
|
|
|
fs.Errorf(fullPath, "%v", err)
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
err = operations.Purge(context.Background(), dir, "")
|
2019-02-07 19:29:52 +08:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "Purge failed")
|
|
|
|
lastErr = err
|
|
|
|
fs.Errorf(dir, "%v", err)
|
|
|
|
return nil
|
2018-09-29 21:48:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2019-02-07 19:29:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return lastErr
|
2018-09-29 21:48:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanRemotes cleans the list of remotes passed in
|
|
|
|
func cleanRemotes(remotes []string) error {
|
|
|
|
var lastError error
|
|
|
|
for _, remote := range remotes {
|
|
|
|
log.Printf("%q - Cleaning", remote)
|
|
|
|
err := cleanFs(remote)
|
|
|
|
if err != nil {
|
|
|
|
lastError = err
|
|
|
|
log.Printf("Failed to purge %q: %v", remote, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lastError
|
|
|
|
}
|