aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2020-09-27 21:16:25 -0400
committerMitch Riedstra <mitch@riedstra.us>2020-09-27 21:16:25 -0400
commit9a8ca79ddb3b5f309c1ee6c7a8713a38fc81fe2b (patch)
treefa2e2f93a3c36860bdba15705b4a687d21d26498
parented8ece2154abac85660dbc25fbcf5e069f779913 (diff)
downloaddeduplicator-9a8ca79ddb3b5f309c1ee6c7a8713a38fc81fe2b.tar.gz
deduplicator-9a8ca79ddb3b5f309c1ee6c7a8713a38fc81fe2b.tar.xz
Make the script output work for when you're removing files as well
-rw-r--r--main.go76
1 files changed, 57 insertions, 19 deletions
diff --git a/main.go b/main.go
index 3bfa10c..2b032be 100644
--- a/main.go
+++ b/main.go
@@ -56,6 +56,26 @@ type ChecksummerInput struct {
Info os.FileInfo
}
+type ChecksumSlice []*ChecksummerResult
+
+func (c ChecksumSlice) BuildRemoveList(reg *regexp.Regexp, matchPath bool) ChecksumSlice {
+ removing := ChecksumSlice{}
+ for _, result := range c {
+ s := ""
+ if !matchPath {
+ s = result.Info.Name()
+ } else {
+ s = result.Path
+ }
+
+ if reg.MatchString(s) {
+ removing = append(removing, result)
+ }
+
+ }
+ return removing
+}
+
// Worker that takes in a filepath off of a channel and outputs the checksum,
// and any errors
func Checksummer(done chan<- bool, input <-chan *ChecksummerInput, results chan<- *ChecksummerResult) {
@@ -145,7 +165,7 @@ func main() {
log.Fatalf("Error compiling provided regular expression: %s", err)
}
- checksums := make(map[string][]*ChecksummerResult)
+ checksums := make(map[string]ChecksumSlice)
pths := make(chan *ChecksummerInput)
results := make(chan *ChecksummerResult)
done := make(chan bool)
@@ -191,27 +211,43 @@ wait:
}
switch {
- case *script:
- for _, result := range list {
- fmt.Printf("%s::%d::%s\n", sum, result.Info.Size(), result.Path)
+ case *script && *remove:
+ removing := list.BuildRemoveList(removeRegex, *matchPath)
+
+ if len(removing) >= len(list) || len(removing) == 0 {
+ for _, result := range list {
+ fmt.Printf("%s::%d::%s::%s\n",
+ sum,
+ result.Info.Size(),
+ result.Path,
+ "not removing")
+ }
+ continue
}
- case *remove:
-
- removing := []*ChecksummerResult{}
- for _, result := range list {
- s := ""
- if !*matchPath {
- s = result.Info.Name()
- } else {
- s = result.Path
+ if !*removeYes {
+ for _, f := range removing {
+ fmt.Printf("%s::%d::%s::%s\n",
+ sum,
+ f.Info.Size(),
+ f.Path,
+ "would remove")
}
-
- if removeRegex.MatchString(s) {
- removing = append(removing, result)
+ } else {
+ for _, f := range removing {
+ fmt.Printf("%s::%d::%s::%s\n",
+ sum,
+ f.Info.Size(),
+ f.Path,
+ "removing")
+ err := os.Remove(f.Path)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ }
}
-
}
+ case *remove:
+ removing := list.BuildRemoveList(removeRegex, *matchPath)
if len(removing) >= len(list) {
fmt.Printf("%s: Not removing, matches all files\n", sum)
@@ -236,12 +272,14 @@ wait:
}
}
}
-
+ case *script:
+ for _, result := range list {
+ fmt.Printf("%s::%d::%s\n", sum, result.Info.Size(), result.Path)
+ }
default:
fmt.Println(sum)
for i, result := range list {
if i == 0 {
- // fmt.Printf("%d kbytes\n", result.Info.Size()/1024)
fmt.Println(formatBytes(result.Info.Size()))
}
fmt.Println("\t" + result.Path)