save redundancies on exit
This commit is contained in:
parent
5800f2229f
commit
63c1c88393
@ -38,14 +38,18 @@ namespace RedundancyFinderCLI
|
|||||||
}
|
}
|
||||||
public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
|
public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
var extensions = settings.Extensions.Split(",", StringSplitOptions.RemoveEmptyEntries);
|
var extensions = settings.Extensions.Split(",", StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
Finder finder = new Finder();
|
Finder finder = new Finder();
|
||||||
int hashingTasks = 0;
|
int hashingTasks = 0;
|
||||||
int hashingTasksFinished = 0;
|
int hashingTasksFinished = 0;
|
||||||
|
|
||||||
|
// Register the ProcessExit event to save redundancies on exit
|
||||||
|
AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
|
||||||
|
{
|
||||||
|
SaveRedundancies(finder, settings.OutputPath);
|
||||||
|
};
|
||||||
|
|
||||||
finder.FileError += (sender, e) =>
|
finder.FileError += (sender, e) =>
|
||||||
{
|
{
|
||||||
if (e.Exception is UnauthorizedAccessException)
|
if (e.Exception is UnauthorizedAccessException)
|
||||||
@ -93,12 +97,8 @@ namespace RedundancyFinderCLI
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var p = AnsiConsole.Progress();
|
var p = AnsiConsole.Progress();
|
||||||
p
|
p.Start(ctx =>
|
||||||
.Start(ctx =>
|
|
||||||
{
|
{
|
||||||
// Define tasks
|
|
||||||
//var hashing = ctx.AddTask("[green]Hashing Files[/]",autoStart:false,);
|
|
||||||
|
|
||||||
finder.TaskStarted += (sender, e) =>
|
finder.TaskStarted += (sender, e) =>
|
||||||
{
|
{
|
||||||
hashingTasks++;
|
hashingTasks++;
|
||||||
@ -106,7 +106,6 @@ namespace RedundancyFinderCLI
|
|||||||
{
|
{
|
||||||
WriteLine($"[green]Task started: [/]{e}");
|
WriteLine($"[green]Task started: [/]{e}");
|
||||||
}
|
}
|
||||||
//hashing.MaxValue = hashingTasks;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
finder.FileFound += (sender, e) =>
|
finder.FileFound += (sender, e) =>
|
||||||
@ -116,30 +115,17 @@ namespace RedundancyFinderCLI
|
|||||||
WriteLine($"[green]File found: [/]{e.FilePath} {GetSizeFormat((ulong)e.Size)} ");
|
WriteLine($"[green]File found: [/]{e.FilePath} {GetSizeFormat((ulong)e.Size)} ");
|
||||||
}
|
}
|
||||||
hashingTasksFinished++;
|
hashingTasksFinished++;
|
||||||
//hashing.Value = hashingTasksFinished;
|
|
||||||
};
|
};
|
||||||
//hashing.Value = hashing.MaxValue;
|
|
||||||
finder.FindRedundancies(settings.SearchPaths, extensions);
|
finder.FindRedundancies(settings.SearchPaths, extensions);
|
||||||
//hashing.StopTask();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var json = JsonConvert.SerializeObject(finder.Redundancies, Formatting.Indented);
|
SaveRedundancies(finder, settings.OutputPath);
|
||||||
|
|
||||||
var outputPath = settings.OutputPath;
|
|
||||||
//check if path is relative or absolute
|
|
||||||
if (!Path.IsPathRooted(settings.OutputPath))
|
|
||||||
{
|
|
||||||
outputPath = Path.Combine(Directory.GetCurrentDirectory(), outputPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
File.WriteAllText(outputPath, json);
|
|
||||||
|
|
||||||
WriteLine($"[yellow]Wrote [/]{finder.Redundancies.Count}[yellow] redundancies to [/]'{outputPath}'");
|
|
||||||
|
|
||||||
ulong totalSize = finder.Redundancies.Select(x => (ulong)x.Value.FileSize).Aggregate((a, b) => a + b);
|
ulong totalSize = finder.Redundancies.Select(x => (ulong)x.Value.FileSize).Aggregate((a, b) => a + b);
|
||||||
string sizeFormat = GetSizeFormat(totalSize);
|
string sizeFormat = GetSizeFormat(totalSize);
|
||||||
WriteLine($"Total Size: [green]{sizeFormat}[/]");
|
WriteLine($"Total Size: [green]{sizeFormat}[/]");
|
||||||
|
|
||||||
if (settings.Verbose)
|
if (settings.Verbose)
|
||||||
{
|
{
|
||||||
foreach (var redundancy in finder.Redundancies.Values)
|
foreach (var redundancy in finder.Redundancies.Values)
|
||||||
@ -152,17 +138,36 @@ namespace RedundancyFinderCLI
|
|||||||
}
|
}
|
||||||
AnsiConsole.WriteLine();
|
AnsiConsole.WriteLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
WriteLine($"[red] Error:\n[/]{e.Message}");
|
WriteLine($"[red] Error:\n[/]{e.Message}");
|
||||||
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void SaveRedundancies(Finder finder, string outputPath)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var json = JsonConvert.SerializeObject(finder.Redundancies, Formatting.Indented);
|
||||||
|
|
||||||
|
// Check if path is relative or absolute
|
||||||
|
if (!Path.IsPathRooted(outputPath))
|
||||||
|
{
|
||||||
|
outputPath = Path.Combine(Directory.GetCurrentDirectory(), outputPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
File.WriteAllText(outputPath, json);
|
||||||
|
WriteLine($"[yellow]Wrote [/]{finder.Redundancies.Count}[yellow] redundancies to [/]'{outputPath}'");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
WriteLine($"[red]Failed to save redundancies: {ex.Message}[/]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void WriteLine(string v)
|
private void WriteLine(string v)
|
||||||
{
|
{
|
||||||
string now = Markup.Escape($"[{DateTime.Now.ToString("HH:mm:ss")}]");
|
string now = Markup.Escape($"[{DateTime.Now.ToString("HH:mm:ss")}]");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user