-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
72 lines (58 loc) · 2.19 KB
/
Program.cs
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
using Newtonsoft.Json;
using SnapCLI;
internal class Program
{
public enum OutputFormat
{
Text,
JSON
}
[RootCommand(Description = "Search for duplicate files in specified directory")]
public static async ValueTask<int> FindDuplicateFiles(
[Argument(Description = "Directory to search for duplicate files")]
DirectoryInfo path,
[Option(Name = "out", Aliases = "o", HelpName = "filepath", Description = "Output file path")]
FileInfo? outFile = null,
[Option(Name = "format", Aliases = "f", Description = "Set output format")]
OutputFormat format = OutputFormat.Text,
[Option(Name = "hardlinks", Aliases = "h", Description = "Include hardlinks")]
bool includeHardlinks = false
)
{
// create async task
var findDuplicatesTask = FileUtils.FindDuplicateFiles(path.FullName, includeHardlinks);
// report counters wile task is running
while (!findDuplicatesTask.IsCompleted)
{
// report progress
Counters.Log(newline: false);
await Task.WhenAny([findDuplicatesTask, Task.Delay(100)]);
}
// get results
var duplicateFiles = await findDuplicatesTask;
Counters.Log();
// output results to file or console
using var textWriter = outFile != null ? new StreamWriter(outFile.FullName) : Console.Out;
switch (format)
{
case OutputFormat.JSON:
new JsonTextWriter(textWriter).WriteValue(duplicateFiles);
break;
case OutputFormat.Text:
foreach (var group in duplicateFiles)
{
textWriter.WriteLine($"Hash: {group.Key}:");
foreach (var file in group)
textWriter.WriteLine($"\t{file.FullName}");
}
break;
default:
throw new NotImplementedException();
}
// repeat counter after files
if (outFile == null && duplicateFiles.Any())
Counters.Log();
Console.WriteLine($"Duplicates: {duplicateFiles.Sum(g => g.Count())}");
return 0;
}
}