-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (174 loc) · 5.28 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"flag"
"fmt"
"log"
"os"
"regexp"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/ricci2511/filecollate"
)
func main() {
cfg := filecollate.Cfg{}
cfg.KeyGenerator = keyGeneratorSelect()
flag.Var(&cfg.Paths, "p", "paths to search for duplicates")
flag.BoolVar(&cfg.SkipSubdirs, "sd", false, "skip directories traversal")
flag.BoolVar(&cfg.HiddenInclude, "ih", false, "ignore hidden files and directories")
flag.Var(&cfg.ExtInclude, "ie", "extensions to include")
flag.Var(&cfg.ExtExclude, "ee", "extensions to exclude")
flag.Var(&cfg.DirsExclude, "ed", "directories or subdirectories to exclude")
flag.IntVar(&cfg.Workers, "w", 0, "number of workers (defaults to GOMAXPROCS)")
logPaths := flag.Bool("l", false, "duplicate results will be logged to stdout")
flag.Parse()
// When logging, loading spinner is redundant.
var done chan struct{}
if !*logPaths {
done = make(chan struct{})
go loadingSpinner(done)
}
dupes := []string{}
dupesChan := make(chan []string, 10)
// Start the duplicate search in its own goroutine.
go func(cfg filecollate.Cfg, dupesChan chan []string) {
err := filecollate.StreamResults(cfg, dupesChan)
if err != nil {
log.Println(err)
}
}(cfg, dupesChan)
// Append a human readable size to each received duplicate path.
for dupePaths := range dupesChan {
for _, path := range dupePaths {
fi, err := os.Stat(path)
if err != nil {
log.Println(err)
continue
}
s := fmt.Sprintf("%s (%s)", path, humanReadableSize(fi.Size()))
if *logPaths {
fmt.Println(s)
}
dupes = append(dupes, s)
}
}
// Close done channel right after all duplicates have been found.
if done != nil {
close(done)
}
if len(dupes) == 0 {
fmt.Printf("\nNo duplicates found with the provided configuration: %s\n", cfg.String())
os.Exit(0)
}
prompt := &survey.MultiSelect{
Message: "Delete selected files:",
Options: dupes,
PageSize: 10,
}
selectedDupes := []string{}
err := survey.AskOne(prompt, &selectedDupes)
if err != nil {
log.Fatal(err)
}
sizeSuffixRegex := regexp.MustCompile(` \(.+\)$`)
for _, path := range selectedDupes {
path = sizeSuffixRegex.ReplaceAllString(path, "")
err := os.Remove(path)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Deleted: %s\n", path)
}
}
var earthSpinner = []string{"🌍", "🌎", "🌏"}
func loadingSpinner(done <-chan struct{}) {
i := 0
l := len(earthSpinner)
// Return when done channel is closed, otherwise keep spinning.
for {
select {
case <-done:
return
default:
fmt.Printf("\rScanning... %s", earthSpinner[i])
i = (i + 1) % l
time.Sleep(150 * time.Millisecond)
}
}
}
func humanReadableSize(size int64) string {
const unit = 1024
if size < unit {
return fmt.Sprintf("%d B", size)
}
// Calculate the divisor and exponent of the unit symbol to use (KiB, MiB, GiB, etc).
div, exp := int64(unit), 0
for n := size / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(size)/float64(div), "KMGTPE"[exp])
}
type keyGeneratorPair struct {
fn filecollate.KeyGeneratorFunc
description string
}
var keygenMap = map[string]keyGeneratorPair{
"MovieTvFileNamesKeyGenerator": {
description: "Detects and groups the same movie/tv shows based on the file name.",
fn: movieTvFileNamesKeyGenerator, // custom key generator function
},
"AudioCodecKeyGenerator": {
description: "Groups video files together based on their audio codec.",
fn: audioCodecKeyGenerator(""), // custom key generator function (closure)
},
"Crc32HashKeyGenerator": {
description: "Generates a crc32 hash of the first 16KB of the file contents.",
fn: filecollate.Crc32HashKeyGenerator,
},
"FullCrc32HashKeyGenerator": {
description: "Generates a crc32 hash of the entire file contents. Slower, but more accurate.",
fn: filecollate.FullCrc32HashKeyGenerator,
},
"Sha256HashKeyGenerator": {
description: "Generates a sha256 hash of the first 16KB of the file contents.",
fn: filecollate.Sha256HashKeyGenerator,
},
"FullSha256HashKeyGenerator": {
description: "Generates a sha256 hash of the entire file contents. Slower, but more accurate.",
fn: filecollate.FullSha256HashKeyGenerator,
},
}
// Prompts the user to select a key generator function and returns it.
func keyGeneratorSelect() filecollate.KeyGeneratorFunc {
var keygenFnNames []string
for fnName := range keygenMap {
keygenFnNames = append(keygenFnNames, fnName)
}
prompt := &survey.Select{
Message: "Select a key generator function:",
Options: keygenFnNames,
Description: func(val string, _ int) string {
return keygenMap[val].description
},
}
var keygenFnName string
err := survey.AskOne(prompt, &keygenFnName)
if err != nil {
log.Fatal(err)
}
if keygenFnName == "AudioCodecKeyGenerator" {
// Make the user input the audio codec to group files by.
prompt := &survey.Input{
Message: "Enter the audio codec to group files by:",
Help: "Examples: aac, ac3, dts, mp3, vorbis, flac, opus, etc.",
}
var audioCodec string
err := survey.AskOne(prompt, &audioCodec)
if err != nil {
log.Fatal(err)
}
// Return a closure that returns a KeyGeneratorFunc with the provided audio codec.
return audioCodecKeyGenerator(audioCodec)
}
return keygenMap[keygenFnName].fn
}