dedupe/main.go

73 lines
2 KiB
Go
Raw Permalink Normal View History

2023-08-11 13:34:54 +02:00
package main
import (
2023-08-15 14:51:54 +02:00
"os"
"strconv"
"time"
2023-08-11 13:34:54 +02:00
"velvettear/dedupe/files"
2023-08-15 14:51:54 +02:00
"velvettear/dedupe/log"
"velvettear/dedupe/prompts"
2023-08-11 13:34:54 +02:00
"velvettear/dedupe/settings"
)
func main() {
2023-08-15 14:51:54 +02:00
timestamp := time.Now()
2023-08-11 13:34:54 +02:00
settings.Initialize()
2023-08-15 14:51:54 +02:00
log.Info("starting dedupe...")
2023-08-11 13:34:54 +02:00
files.Scan()
2023-08-15 14:51:54 +02:00
if len(files.Duplicates) == 0 {
log.Info("no duplicate files have been found - exiting...")
os.Exit(0)
}
prompts.ListDuplicates()
var proceed bool
var deleteDuplicates bool
if len(settings.MoveDirectory) == 0 && !settings.Delete {
exit(timestamp, 0)
}
if len(settings.MoveDirectory) > 0 && settings.Delete {
proceed, deleteDuplicates = prompts.DeleteOrMove()
} else {
proceed = prompts.HandleDuplicates(settings.Delete)
}
if !proceed {
exit(timestamp, 0)
}
counter := 0
subtimestamp := time.Now()
if deleteDuplicates {
log.Info("deleting duplicate files in '" + settings.ComparisonDirectory + "'...")
for _, file := range files.Duplicates {
subsubtimestamp := time.Now()
error := os.Remove(file)
if error != nil {
log.Error("encountered an error deleting the file '"+file+"'", error.Error())
continue
}
counter++
log.DebugTimed("deleted the file '"+file+"'", subsubtimestamp.UnixMilli())
}
log.InfoTimed("deleted "+strconv.Itoa(counter)+" duplicate files to '"+settings.MoveDirectory+"'", subtimestamp.UnixMilli())
} else {
log.Info("moving duplicate files to '" + settings.MoveDirectory + "'...")
for _, file := range files.Duplicates {
if files.MoveFile(file) {
counter++
}
}
log.InfoTimed("moved "+strconv.Itoa(counter)+" duplicate files to '"+settings.MoveDirectory+"'", subtimestamp.UnixMilli())
}
if prompts.DeleteEmptyDirectories(settings.SourceDirectory) {
files.RemoveEmptyDirectories(settings.SourceDirectory)
}
if prompts.DeleteEmptyDirectories(settings.ComparisonDirectory) {
files.RemoveEmptyDirectories(settings.ComparisonDirectory)
}
exit(timestamp, 0)
}
func exit(timestamp time.Time, code int) {
log.InfoTimed("dedupe finished - exiting...", timestamp.UnixMilli())
os.Exit(code)
2023-08-11 13:34:54 +02:00
}