ConnectionAddCommand.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.ComponentModel;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using RackPeek.Domain.Resources.Connections;
  4. using Spectre.Console;
  5. using Spectre.Console.Cli;
  6. namespace Shared.Rcl.Commands.Connections;
  7. public class ConnectionAddSettings : CommandSettings
  8. {
  9. [CommandArgument(0, "<A_RESOURCE>")]
  10. [Description("Resource name for endpoint A.")]
  11. public string AResource { get; set; } = null!;
  12. [CommandArgument(1, "<A_GROUP>")]
  13. [Description("Port group index for endpoint A.")]
  14. public int AGroup { get; set; }
  15. [CommandArgument(2, "<A_INDEX>")]
  16. [Description("Port index for endpoint A.")]
  17. public int AIndex { get; set; }
  18. [CommandArgument(3, "<B_RESOURCE>")]
  19. [Description("Resource name for endpoint B.")]
  20. public string BResource { get; set; } = null!;
  21. [CommandArgument(4, "<B_GROUP>")]
  22. [Description("Port group index for endpoint B.")]
  23. public int BGroup { get; set; }
  24. [CommandArgument(5, "<B_INDEX>")]
  25. [Description("Port index for endpoint B.")]
  26. public int BIndex { get; set; }
  27. [CommandOption("--label")]
  28. [Description("Optional label for the connection.")]
  29. public string? Label { get; set; }
  30. [CommandOption("--notes")]
  31. [Description("Optional notes for the connection.")]
  32. public string? Notes { get; set; }
  33. }
  34. public class ConnectionAddCommand(
  35. IServiceProvider serviceProvider
  36. ) : AsyncCommand<ConnectionAddSettings>
  37. {
  38. public override async Task<int> ExecuteAsync(
  39. CommandContext context,
  40. ConnectionAddSettings settings,
  41. CancellationToken cancellationToken)
  42. {
  43. using IServiceScope scope = serviceProvider.CreateScope();
  44. IAddConnectionUseCase useCase =
  45. scope.ServiceProvider.GetRequiredService<IAddConnectionUseCase>();
  46. var a = new PortReference
  47. {
  48. Resource = settings.AResource,
  49. PortGroup = settings.AGroup,
  50. PortIndex = settings.AIndex
  51. };
  52. var b = new PortReference
  53. {
  54. Resource = settings.BResource,
  55. PortGroup = settings.BGroup,
  56. PortIndex = settings.BIndex
  57. };
  58. await useCase.ExecuteAsync(
  59. a,
  60. b,
  61. settings.Label,
  62. settings.Notes
  63. );
  64. AnsiConsole.MarkupLine(
  65. $"[green]Connection created:[/] " +
  66. $"{settings.AResource}:{settings.AGroup}:{settings.AIndex} " +
  67. $"<-> " +
  68. $"{settings.BResource}:{settings.BGroup}:{settings.BIndex}"
  69. );
  70. return 0;
  71. }
  72. }