DesktopDriveAddCommand.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 DesktopDriveAddSettings : CommandSettings {
  9. [CommandArgument(0, "<desktop>")]
  10. [Description("The name of the desktop.")]
  11. public string DesktopName { get; set; } = default!;
  12. [CommandOption("--type")]
  13. [Description("The drive type e.g hdd / ssd.")]
  14. public string? Type { get; set; }
  15. [CommandOption("--size")]
  16. [Description("The drive capacity in GB.")]
  17. public int? Size { get; set; }
  18. }
  19. public class DesktopDriveAddCommand(IServiceProvider provider)
  20. : AsyncCommand<DesktopDriveAddSettings> {
  21. public override async Task<int> ExecuteAsync(
  22. CommandContext context,
  23. DesktopDriveAddSettings settings,
  24. CancellationToken cancellationToken) {
  25. using IServiceScope scope = provider.CreateScope();
  26. IAddDriveUseCase<Desktop> useCase = scope.ServiceProvider.GetRequiredService<IAddDriveUseCase<Desktop>>();
  27. await useCase.ExecuteAsync(settings.DesktopName, settings.Type, settings.Size);
  28. AnsiConsole.MarkupLine($"[green]Drive added to desktop '{settings.DesktopName}'.[/]");
  29. return 0;
  30. }
  31. }