-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
71 lines (55 loc) · 1.78 KB
/
diff.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
package main
import (
"fmt"
"strings"
"time"
"github.com/c-bata/go-prompt"
"github.com/rebel-l/go-utils/pb"
"github.com/rebel-l/mp3sync/filesync"
"github.com/rebel-l/mp3sync/mp3files"
)
func diff(source, destination mp3files.Files) filesync.Files {
_, _ = description.Println("Create diff of files to be synced ...")
start := time.Now()
defer fmt.Println()
var syncFiles filesync.Files
count := len(source) + len(destination)
bar := pb.New(pb.EngineCheggaaa, count)
defer bar.Finish()
for destinationName, s := range source {
bar.Increment()
d, ok := destination[destinationName]
// if destination doesn't exist just add it
if !ok {
syncFiles = append(syncFiles, filesync.File{Source: s, Destination: mp3files.File{Name: destinationName}, Operation: filesync.OperationCreate})
continue
}
// if destination exists it must be out of sync
newFile := filesync.File{Source: s, Destination: d, Operation: filesync.OperationUpdate}
if !newFile.IsInSync() {
syncFiles = append(syncFiles, newFile)
}
}
for _, d := range destination {
bar.Increment()
// files not in source needs to be deleted
_, ok := source[d.Name]
if !ok {
syncFiles = append(syncFiles, filesync.File{Destination: d, Operation: filesync.OperationDelete})
}
}
duration(start, time.Now(), fmt.Sprintf("diff for %d files created", count))
return syncFiles
}
func listDiff(files filesync.Files) {
_, _ = listFormat.Printf("Total files to sync: %d\n", len(files))
t := prompt.Input("Show Diff? [Y/n] ", func(d prompt.Document) []prompt.Suggest {
return prompt.FilterHasPrefix([]prompt.Suggest{}, d.GetWordBeforeCursor(), true)
})
if strings.ToLower(t) != "n" {
for _, v := range files {
_, _ = listFormat.Printf("%s: %q\n", v.Operation, v.Destination.Name)
}
}
fmt.Println()
}