/
gourd.go
68 lines (58 loc) · 1.5 KB
/
gourd.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package gourd
import (
"fmt"
"os"
"strings"
)
type (
// File is a path and FileInfo.
File struct {
Path string
FileInfo os.FileInfo
DuplicatePaths []string
}
// Bucket is a list of Files sharing common attributes
Bucket []File
// Buckets are a map of a common attributes to a Bucket. The key is determined by the Bucketer, but must include the current Bucket name.
Buckets map[string]Bucket
// Bucketer receives Buckets and returns a set of Buckets.
// Bucketers should ignore a Bucket with `len(in[key]) < 2`.
// Similarly, Bucketers may omit a Bucket with less than 2 items in the returned Buckets, but are not required to.
Bucketer interface {
Bucket(in Buckets) (Buckets, error)
}
)
// SubBucketName makes consistent bucket namings.
func SubBucketName(currentBucketName, newBucketName string) string {
return fmt.Sprintf("%s::%s", currentBucketName, newBucketName)
}
func (b Bucket) TotalFileSize() int64 {
var totalSize int64
for _, file := range b {
totalSize += file.FileInfo.Size()
}
return totalSize
}
// PossibleDuplicates returns Buckets containing at least two entries.
func (b Buckets) PossibleDuplicates() Buckets {
out := Buckets{}
for k, v := range b {
if len(v) > 1 {
out[k] = v
}
}
return out
}
func (b Buckets) String() string {
sb := new(strings.Builder)
for k, v := range b {
sb.WriteString(k)
sb.WriteRune('\n')
for _, f := range v {
sb.WriteString(" ")
sb.WriteString(f.Path)
sb.WriteRune('\n')
}
}
return sb.String()
}