ConnectionRemoveCommand.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.ComponentModel;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using RackPeek.Domain.Resources.Connections;
  4. using RackPeek.Domain.Resources.SubResources;
  5. using Spectre.Console;
  6. using Spectre.Console.Cli;
  7. namespace Shared.Rcl.Commands.Connections;
  8. public class ConnectionRemoveSettings : CommandSettings
  9. {
  10. [CommandArgument(0, "<RESOURCE>")]
  11. [Description("Resource name.")]
  12. public string Resource { get; set; } = null!;
  13. [CommandArgument(1, "<PORT_GROUP>")]
  14. [Description("Port group index.")]
  15. public int PortGroup { get; set; }
  16. [CommandArgument(2, "<PORT_INDEX>")]
  17. [Description("Port index.")]
  18. public int PortIndex { get; set; }
  19. }
  20. public class ConnectionRemoveCommand(
  21. IServiceProvider serviceProvider
  22. ) : AsyncCommand<ConnectionRemoveSettings>
  23. {
  24. public override async Task<int> ExecuteAsync(
  25. CommandContext context,
  26. ConnectionRemoveSettings settings,
  27. CancellationToken cancellationToken)
  28. {
  29. using IServiceScope scope = serviceProvider.CreateScope();
  30. IRemoveConnectionUseCase useCase =
  31. scope.ServiceProvider.GetRequiredService<IRemoveConnectionUseCase>();
  32. var port = new PortReference
  33. {
  34. Resource = settings.Resource,
  35. PortGroup = settings.PortGroup,
  36. PortIndex = settings.PortIndex
  37. };
  38. await useCase.ExecuteAsync(port);
  39. AnsiConsole.MarkupLine(
  40. $"[green]Connection removed from[/] " +
  41. $"{settings.Resource}:{settings.PortGroup}:{settings.PortIndex}"
  42. );
  43. return 0;
  44. }
  45. }