DesktopDriveRemoveCommand.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System.ComponentModel;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using RackPeek.Domain.Resources.Desktops;
  4. using RackPeek.Domain.UseCases.Drives;
  5. using Spectre.Console;
  6. using Spectre.Console.Cli;
  7. namespace Shared.Rcl.Commands.Desktops.Drive;
  8. public class DesktopDriveRemoveSettings : CommandSettings
  9. {
  10. [CommandArgument(0, "<desktop>")]
  11. [Description("The name of the desktop.")]
  12. public string DesktopName { get; set; } = default!;
  13. [CommandArgument(1, "<index>")]
  14. [Description("The index of the drive to remove.")]
  15. public int Index { get; set; }
  16. }
  17. public class DesktopDriveRemoveCommand(IServiceProvider provider)
  18. : AsyncCommand<DesktopDriveRemoveSettings>
  19. {
  20. public override async Task<int> ExecuteAsync(
  21. CommandContext context,
  22. DesktopDriveRemoveSettings settings,
  23. CancellationToken cancellationToken)
  24. {
  25. using var scope = provider.CreateScope();
  26. var useCase = scope.ServiceProvider.GetRequiredService<IRemoveDriveUseCase<Desktop>>();
  27. await useCase.ExecuteAsync(settings.DesktopName, settings.Index);
  28. AnsiConsole.MarkupLine($"[green]Drive #{settings.Index} removed from desktop '{settings.DesktopName}'.[/]");
  29. return 0;
  30. }
  31. }