-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorter.go
82 lines (72 loc) · 1.89 KB
/
sorter.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"io"
"log"
"os"
"path"
"path/filepath"
"sync"
"github.com/superhawk610/bar"
)
// copy a file to bucket
func copyFileToBucket(filePath string, bucket *Bucket) error {
sourceFile, err := os.Open(filePath)
if err != nil {
log.Printf("Failed to open file %s due to %s", filePath, err)
return err
}
defer sourceFile.Close()
destPath := path.Join(bucket.Path, filepath.Base(filePath))
destFile, err := os.Create(destPath)
if err != nil {
log.Printf("Failed to create file at %s due to %s", destPath, err)
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, sourceFile)
if err != nil {
log.Printf("Failed to copy %s to bucket %s", filePath, bucket.Name)
return err
}
return nil
}
// find files belonging to a bucket
func SortFilesIntoBuckets(buckets *Buckets, dir string, doClean bool) {
var wg sync.WaitGroup
for _, bucket := range *buckets {
// log.Println("Sorting bucket", bucket.Name)
currentBucket := bucket
wg.Add(1)
go func(wg *sync.WaitGroup, bucket *Bucket, dir string) {
defer wg.Done()
filesToCopy := bucket.FindAllFiles(dir)
filesToCopyCount := len(filesToCopy)
// log.Printf("Found %d files to be copied to %s", filesToCopyCount, bucket.Name)
if filesToCopyCount == 0 {
return
}
if !bucket.Exists() {
err := bucket.CreateDir()
if err != nil {
log.Fatalf("Failed to create %s due to %s", bucket.Name, err)
}
}
copyBar := bar.NewWithOpts(
bar.WithDimensions(filesToCopyCount, 30),
bar.WithFormat(fmt.Sprintf("Copying to [:bucket] :bar (:percent) of %d files", filesToCopyCount)),
)
for _, filePath := range filesToCopy {
err := copyFileToBucket(filePath, bucket)
if err == nil && doClean {
os.Remove(filePath)
}
copyBar.TickAndUpdate(bar.Context{
bar.Ctx("bucket", bucket.Name),
})
}
copyBar.Done()
}(&wg, ¤tBucket, dir)
}
wg.Wait()
}