-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete.go
43 lines (36 loc) · 1.08 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package scrubber
import (
"os"
)
// deleteAction represents the action of deleting old or big files.
type deleteAction struct {
action
}
// newDeleteAction returns a pointer to a deleteAction.
func newDeleteAction(dir *directory, fs Filesystem, log logger, pretend bool) *deleteAction {
return &deleteAction{action{dir, fs, log, pretend}}
}
// perform deletes files that are past a certain age or certain size.
func (a deleteAction) perform(files []os.FileInfo, check checkFn) ([]os.FileInfo, error) {
var newFiles []os.FileInfo
for _, file := range files {
file := file
filename := a.fs.FullPath(file, a.dir.Path)
if check(file) {
if a.pretend {
a.log.Printf("[Delete] PRETEND: Would delete file %s", filename)
continue
}
a.log.Printf("[Delete] Deleting file %s", filename)
err := a.fs.Remove(filename)
if err != nil {
a.log.Printf("[Delete] ERROR: Failed to delete file %s: %s", filename, err)
continue
}
} else {
a.log.Printf("[Delete] No action is needed for file %s", filename)
newFiles = append(newFiles, file)
}
}
return newFiles, nil
}