-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsize.go
53 lines (42 loc) · 1.2 KB
/
size.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
44
45
46
47
48
49
50
51
52
53
package scrubber
import (
"fmt"
"os"
"github.com/c2h5oh/datasize"
)
// ageStrategy represents the action of deleting files that are greater than a certain file size.
type sizeStrategy struct {
Strategy
limit int64
}
// newSizeStrategy returns a new sizeStrategy.
func newSizeStrategy(c *StrategyConfig, dir *directory, action performer, log logger) *sizeStrategy {
return &sizeStrategy{Strategy{c, dir, action, log}, 0}
}
// process deletes all files greater than a certain size.
func (s sizeStrategy) process(files []os.FileInfo) ([]os.FileInfo, error) {
err := s.unmarshalText([]byte(s.c.Limit))
if err != nil {
return nil, err
}
if s.limit <= 0 {
return nil, fmt.Errorf("file size limit has to be greater than 0")
}
files, err = s.action.perform(files, func(file os.FileInfo) bool {
return file.Size() > s.limit
})
return files, err
}
// parseLimit turns a string representation of a filesize into bytes
func (s *sizeStrategy) unmarshalText(text []byte) error {
if len(text) < 1 {
return fmt.Errorf("limit cannot be an empty string")
}
var v datasize.ByteSize
err := v.UnmarshalText(text)
if err != nil {
return fmt.Errorf("invalid size definition")
}
s.limit = int64(v)
return nil
}