diff --git a/RedundancyFinderCLI/Properties/launchSettings.json b/RedundancyFinderCLI/Properties/launchSettings.json index dbe6ffb..3a56ab5 100644 --- a/RedundancyFinderCLI/Properties/launchSettings.json +++ b/RedundancyFinderCLI/Properties/launchSettings.json @@ -10,11 +10,11 @@ }, "Delete": { "commandName": "Project", - "commandLineArgs": "delete redundancies.json C:\\Users\\daskn\\Pictures\\B\\ -v" + "commandLineArgs": "delete redundancies.json C:\\Users\\daskn\\Pictures\\ -v" }, "Sanitize": { "commandName": "Project", - "commandLineArgs": "sanitize C:\\Users\\daskn\\Pictures\\ -v" + "commandLineArgs": "sanitize C:\\Users\\daskn\\Pictures\\ -v -d redundancies.json" } } } \ No newline at end of file diff --git a/RedundancyFinderCLI/SanitizeCommand.cs b/RedundancyFinderCLI/SanitizeCommand.cs index 069fc7f..76fb355 100644 --- a/RedundancyFinderCLI/SanitizeCommand.cs +++ b/RedundancyFinderCLI/SanitizeCommand.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Reflection; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; @@ -26,18 +27,63 @@ namespace RedundancyFinderCLI [DefaultValue(false)] public bool Verbose { get; init; } + [Description("Show all information.")] + [CommandOption("-d|--deleteNonExistent ")] + [DefaultValue("redundancies.json")] + public string? PathToSource { get; init; } + } + + Settings settings = null; public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings) { + this.settings = settings; Global.WriteLine($"[white]Sanitizing {settings.Path}[/]"); Global.WriteLine($"[yellow]Deleting empty folders[/]"); int count = DeleteEmptyFolders(settings.Path); - + if (File.Exists(settings.PathToSource)) + { + DeleteNonExistentPaths(); + } Global.WriteLine($"[green]Deleted [/]{count}[green] empty folders[/]"); return 0; } + public void DeleteNonExistentPaths() + { + Dictionary redundancies = null; + + + AnsiConsole.Status() + .Start($"[yellow]Loading [/]'{settings.PathToSource}'", ctx => + { + ctx.Spinner(Spinner.Known.Clock); + ctx.SpinnerStyle = Style.Parse("yellow bold"); + Thread.Sleep(1000); + redundancies = JsonConvert.DeserializeObject>(File.ReadAllText(settings.PathToSource)); + + }); + + foreach (var redundancy in redundancies) + { + var paths = redundancy.Value.Paths; + foreach (var path in paths.ToList()) + { + if (!File.Exists(path)) + { + Global.WriteLine($"[red]Deleting non existent file: {path}[/]"); + + redundancy.Value.Paths.Remove(path); + } + } + } + + var json = JsonConvert.SerializeObject(redundancies, Formatting.Indented); + File.WriteAllText(settings.PathToSource, json); + } + + public static int DeleteEmptyFolders(string directoryPath, int count = 0) { try