feat: enable verbose output to list hash of every file

This commit is contained in:
Oliver Booth 2024-04-17 14:17:56 +01:00
parent d2ca59e174
commit 435318cd20
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
2 changed files with 9 additions and 5 deletions

View File

@ -1,6 +1,4 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Diagnostics;
using System.Security;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using Spectre.Console; using Spectre.Console;
@ -110,7 +108,7 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
{ {
string relativeFilePath = Path.GetRelativePath(inputDirectory.FullName, file.FullName); string relativeFilePath = Path.GetRelativePath(inputDirectory.FullName, file.FullName);
AnsiConsole.MarkupLineInterpolated($"Checking hash for [cyan]{relativeFilePath}[/]"); AnsiConsole.MarkupLineInterpolated($"Checking hash for [cyan]{relativeFilePath}[/]");
tasks.Add(Task.Run(() => ProcessFile(file))); tasks.Add(Task.Run(() => ProcessFile(file, settings)));
} }
} }
catch (Exception ex) catch (Exception ex)
@ -120,7 +118,7 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
} }
} }
private void ProcessFile(FileInfo file) private void ProcessFile(FileInfo file, ListSettings settings)
{ {
Span<byte> buffer = stackalloc byte[64]; Span<byte> buffer = stackalloc byte[64];
try try
@ -129,7 +127,8 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
using BufferedStream bufferedStream = new BufferedStream(stream, 1048576 /* 1MB */); using BufferedStream bufferedStream = new BufferedStream(stream, 1048576 /* 1MB */);
SHA512.HashData(bufferedStream, buffer); SHA512.HashData(bufferedStream, buffer);
string hash = ByteSpanToString(buffer); string hash = ByteSpanToString(buffer);
Trace.WriteLine($"{file.FullName}: {hash}"); if (settings.Verbose)
AnsiConsole.WriteLine($"{file.FullName} ->\n {hash}");
if (!_fileHashMap.TryGetValue(hash, out List<FileInfo>? cache)) if (!_fileHashMap.TryGetValue(hash, out List<FileInfo>? cache))
_fileHashMap[hash] = cache = new List<FileInfo>(); _fileHashMap[hash] = cache = new List<FileInfo>();

View File

@ -13,4 +13,9 @@ internal sealed class ListSettings : CommandSettings
[Description("When this flag is set, the directory will be scanned recursively. This may take longer.")] [Description("When this flag is set, the directory will be scanned recursively. This may take longer.")]
[DefaultValue(false)] [DefaultValue(false)]
public bool Recursive { get; set; } = false; public bool Recursive { get; set; } = false;
[CommandOption("--verbose")]
[Description("Enable verbose output.")]
[DefaultValue(false)]
public bool Verbose { get; set; } = false;
} }