DesktopNicSetCommand.cs 1.6 KB

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