DesktopDriveSetCommand.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 DesktopDriveSetSettings : CommandSettings
  9. {
  10. [CommandArgument(0, "<desktop>")]
  11. [Description("The desktop name.")]
  12. public string DesktopName { get; set; } = default!;
  13. [CommandArgument(1, "<index>")]
  14. [Description("The drive index to update.")]
  15. public int Index { get; set; }
  16. [CommandOption("--type")]
  17. [Description("The drive type e.g hdd / ssd.")]
  18. public string? Type { get; set; }
  19. [CommandOption("--size")]
  20. [Description("The drive capacity in Gb.")]
  21. public int? Size { get; set; }
  22. }
  23. public class DesktopDriveSetCommand(IServiceProvider provider)
  24. : AsyncCommand<DesktopDriveSetSettings>
  25. {
  26. public override async Task<int> ExecuteAsync(
  27. CommandContext context,
  28. DesktopDriveSetSettings settings,
  29. CancellationToken cancellationToken)
  30. {
  31. using var scope = provider.CreateScope();
  32. var useCase = scope.ServiceProvider.GetRequiredService<IUpdateDriveUseCase<Desktop>>();
  33. await useCase.ExecuteAsync(settings.DesktopName, settings.Index, settings.Type, settings.Size);
  34. AnsiConsole.MarkupLine($"[green]Drive #{settings.Index} updated on desktop '{settings.DesktopName}'.[/]");
  35. return 0;
  36. }
  37. }