implemented sanitize command
This commit is contained in:
parent
3834c2fba0
commit
d6480c8e0e
@ -16,6 +16,7 @@ internal class Program
|
|||||||
{
|
{
|
||||||
config.AddCommand<AnalyzeCommand>("analyze");
|
config.AddCommand<AnalyzeCommand>("analyze");
|
||||||
config.AddCommand<DeleteCommand>("delete");
|
config.AddCommand<DeleteCommand>("delete");
|
||||||
|
config.AddCommand<SanitizeCommand>("sanitize");
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
|
|
||||||
config.PropagateExceptions();
|
config.PropagateExceptions();
|
||||||
|
|||||||
@ -11,6 +11,10 @@
|
|||||||
"Delete": {
|
"Delete": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"commandLineArgs": "delete redundancies.json C:\\Users\\daskn\\Pictures\\B\\ -v"
|
"commandLineArgs": "delete redundancies.json C:\\Users\\daskn\\Pictures\\B\\ -v"
|
||||||
|
},
|
||||||
|
"Sanitize": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"commandLineArgs": "sanitize C:\\Users\\daskn\\Pictures\\ -v"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
70
RedundancyFinderCLI/SanitizeCommand.cs
Normal file
70
RedundancyFinderCLI/SanitizeCommand.cs
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using RedundancyFinder;
|
||||||
|
using Spectre.Console;
|
||||||
|
using Spectre.Console.Cli;
|
||||||
|
namespace RedundancyFinderCLI
|
||||||
|
{
|
||||||
|
internal sealed class SanitizeCommand : Command<SanitizeCommand.Settings>
|
||||||
|
{
|
||||||
|
public sealed class Settings : CommandSettings
|
||||||
|
{
|
||||||
|
[Description("Path to sanitize.")]
|
||||||
|
[CommandArgument(0, "[path]")]
|
||||||
|
public string? Path { get; init; }
|
||||||
|
|
||||||
|
[Description("Show all information.")]
|
||||||
|
[CommandOption("-v|--verbose")]
|
||||||
|
[DefaultValue(false)]
|
||||||
|
public bool Verbose { get; init; }
|
||||||
|
|
||||||
|
}
|
||||||
|
public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
|
||||||
|
{
|
||||||
|
Global.WriteLine($"[white]Sanitizing {settings.Path}[/]");
|
||||||
|
Global.WriteLine($"[yellow]Deleting empty folders[/]");
|
||||||
|
int count = DeleteEmptyFolders(settings.Path);
|
||||||
|
|
||||||
|
Global.WriteLine($"[green]Deleted [/]{count}[green] empty folders[/]");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int DeleteEmptyFolders(string directoryPath, int count = 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Get all subdirectories
|
||||||
|
foreach (var subDirectory in Directory.GetDirectories(directoryPath))
|
||||||
|
{
|
||||||
|
// Recursively delete empty folders in subdirectories
|
||||||
|
count = DeleteEmptyFolders(subDirectory, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the current directory is empty
|
||||||
|
if (Directory.GetFiles(directoryPath).Length == 0 && Directory.GetDirectories(directoryPath).Length == 0)
|
||||||
|
{
|
||||||
|
Directory.Delete(directoryPath);
|
||||||
|
count++;
|
||||||
|
Global.WriteLine($"Deleted empty folder: {directoryPath}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Global.WriteLine($"[red]Error deleting folder [/]'{directoryPath}':\n [red]{ex.Message}[/]");
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user