ConnectionRemoveCommand.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 ConnectionRemoveSettings : CommandSettings {
  8. [CommandArgument(0, "<RESOURCE>")]
  9. [Description("Resource name.")]
  10. public string Resource { get; set; } = null!;
  11. [CommandArgument(1, "<PORT_GROUP>")]
  12. [Description("Port group index.")]
  13. public int PortGroup { get; set; }
  14. [CommandArgument(2, "<PORT_INDEX>")]
  15. [Description("Port index.")]
  16. public int PortIndex { get; set; }
  17. }
  18. public class ConnectionRemoveCommand(
  19. IServiceProvider serviceProvider
  20. ) : AsyncCommand<ConnectionRemoveSettings> {
  21. public override async Task<int> ExecuteAsync(
  22. CommandContext context,
  23. ConnectionRemoveSettings settings,
  24. CancellationToken cancellationToken) {
  25. using IServiceScope scope = serviceProvider.CreateScope();
  26. IRemoveConnectionUseCase useCase =
  27. scope.ServiceProvider.GetRequiredService<IRemoveConnectionUseCase>();
  28. var port = new PortReference {
  29. Resource = settings.Resource,
  30. PortGroup = settings.PortGroup,
  31. PortIndex = settings.PortIndex
  32. };
  33. await useCase.ExecuteAsync(port);
  34. AnsiConsole.MarkupLine(
  35. $"[green]Connection removed from[/] " +
  36. $"{settings.Resource}:{settings.PortGroup}:{settings.PortIndex}"
  37. );
  38. return 0;
  39. }
  40. }