ConnectionAddCommand.cs 2.5 KB

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