ConnectionRemoveCommand.cs 1.6 KB

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