-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (97 loc) · 3.07 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// This package's main function copies files from one folder to another but doesn't override files that are the same (files are the same if they have the same size)
package main
import (
"fmt"
"log"
"mirror/mirror"
"os"
)
const (
MsgCanceling = "canceling"
MsgGatheringInfo = "gathering info about files"
MgsAreYouSure = "Do you want to continue?"
MsgLogging = "Also a log file named 'log' will be generated."
MsgNothingToDo = "there is nothing to do"
MsgErrOccurred = "an error occurred:"
MsgFinished = "the program finished successfully"
MsgDone = "done"
)
func main() {
dst, src, cleaningMode, err := mirror.VetFlags()
checkErr(err)
if cleaningMode {
doCleaning(dst, src)
} else {
doCopying(dst, src)
}
log.Println(MsgFinished)
}
func doCopying(dst, src string) {
if !mirror.AskQuestion(fmt.Sprintf("files from %q will be copied to %q. %s", src, dst, MgsAreYouSure)) {
exitWithZero(MsgCanceling)
}
missingFolders, missingFiles, totalSize := srcDstDiff(dst, src, false)
if !mirror.AskQuestion(fmt.Sprintf("%d files will be coppied (%s MB) and %d folders will be created. %s %s", len(missingFiles), mirror.BytesToMB(totalSize), len(missingFolders), MsgLogging, MgsAreYouSure)) {
exitWithZero(MsgCanceling)
}
err := mirror.TruncateLogFile()
checkErr(err)
if len(missingFolders) > 0 {
err = mirror.MakeFolders(missingFolders, dst)
checkErr(err)
log.Println(MsgDone)
}
if len(missingFiles) > 0 {
err = mirror.CopyFiles(missingFiles, totalSize, src, dst)
checkErr(err)
log.Println(MsgDone)
}
}
func doCleaning(dst, src string) {
if !mirror.AskQuestion(fmt.Sprintf("files may be deleted in the %q folder. %s", dst, MgsAreYouSure)) {
exitWithZero(MsgCanceling)
}
foldersToClean, filesToClean, totalSize := srcDstDiff(dst, src, true)
if !mirror.AskQuestion(fmt.Sprintf("%d files (%s MB) and %d folders will be deleted. %s %s", len(filesToClean), mirror.BytesToMB(totalSize), len(foldersToClean), MsgLogging, MgsAreYouSure)) {
exitWithZero(MsgCanceling)
}
err := mirror.TruncateLogFile()
checkErr(err)
if len(filesToClean) > 0 {
err = mirror.CleanFiles(filesToClean, totalSize, dst)
checkErr(err)
log.Println(MsgDone)
}
if len(foldersToClean) > 0 {
err = mirror.CleanFolders(foldersToClean, dst)
checkErr(err)
log.Println(MsgDone)
}
}
func srcDstDiff(dst, src string, cleaningMod bool) (folders mirror.Folder, files mirror.File, totalSize int64) {
log.Println(MsgGatheringInfo)
srcFolders, srcFiles, err := mirror.ReadFolder(src)
checkErr(err)
dstFolders, dstFiles, err := mirror.ReadFolder(dst)
checkErr(err)
if cleaningMod {
folders = mirror.FoldersToClean(dstFolders, srcFolders)
files, totalSize = mirror.FilesToClean(dstFiles, srcFiles)
} else {
folders = mirror.MissingFolders(dstFolders, srcFolders)
files, totalSize = mirror.MissingFiles(dstFiles, srcFiles)
}
if len(files) == 0 && len(folders) == 0 {
exitWithZero(MsgNothingToDo)
}
return
}
func checkErr(err error) {
if err != nil {
log.Fatalln(MsgErrOccurred, err)
}
}
func exitWithZero(msg string) {
log.Println(msg)
os.Exit(0)
}