DesktopNicSetCommand.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.ComponentModel;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using RackPeek.Domain.Resources.Desktops;
  4. using RackPeek.Domain.UseCases.Nics;
  5. using Spectre.Console;
  6. using Spectre.Console.Cli;
  7. namespace Shared.Rcl.Commands.Desktops.Nics;
  8. public class DesktopNicSetSettings : 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 index of the nic to remove.")]
  15. public int Index { get; set; }
  16. [CommandOption("--type")]
  17. [Description("The nic port type e.g rj45 / sfp+")]
  18. public string? Type { get; set; }
  19. [CommandOption("--speed")]
  20. [Description("The speed of the nic in Gb/s.")]
  21. public int? Speed { get; set; }
  22. [CommandOption("--ports")]
  23. [Description("The number of ports.")]
  24. public int? Ports { get; set; }
  25. }
  26. public class DesktopNicSetCommand(IServiceProvider provider)
  27. : AsyncCommand<DesktopNicSetSettings>
  28. {
  29. public override async Task<int> ExecuteAsync(
  30. CommandContext context,
  31. DesktopNicSetSettings settings,
  32. CancellationToken cancellationToken)
  33. {
  34. using var scope = provider.CreateScope();
  35. var useCase = scope.ServiceProvider.GetRequiredService<IUpdateNicUseCase<Desktop>>();
  36. await useCase.ExecuteAsync(settings.DesktopName, settings.Index, settings.Type, settings.Speed, settings.Ports);
  37. AnsiConsole.MarkupLine($"[green]NIC #{settings.Index} updated on desktop '{settings.DesktopName}'.[/]");
  38. return 0;
  39. }
  40. }