4
0

ServiceGetCommand.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Microsoft.Extensions.DependencyInjection;
  2. using RackPeek.Domain.Resources.Services.UseCases;
  3. using Spectre.Console;
  4. using Spectre.Console.Cli;
  5. namespace Shared.Rcl.Commands.Services;
  6. public class ServiceGetCommand(
  7. IServiceProvider serviceProvider
  8. ) : AsyncCommand {
  9. public override async Task<int> ExecuteAsync(
  10. CommandContext context,
  11. CancellationToken cancellationToken) {
  12. using IServiceScope scope = serviceProvider.CreateScope();
  13. ServiceReportUseCase useCase = scope.ServiceProvider.GetRequiredService<ServiceReportUseCase>();
  14. ServiceReport report = await useCase.ExecuteAsync();
  15. if (report.Services.Count == 0) {
  16. AnsiConsole.MarkupLine("[yellow]No Services found.[/]");
  17. return 0;
  18. }
  19. Table table = new Table()
  20. .Border(TableBorder.Rounded)
  21. .AddColumn("Name")
  22. .AddColumn("Ip")
  23. .AddColumn("Port")
  24. .AddColumn("Protocol")
  25. .AddColumn("Url")
  26. .AddColumn("Runs On");
  27. foreach (ServiceReportRow s in report.Services) {
  28. string? sys = null;
  29. string? phys = null;
  30. if (s.RunsOnSystemHost is not null) sys = string.Join(", ", s.RunsOnSystemHost);
  31. if (s.RunsOnPhysicalHost is not null) phys = string.Join(", ", s.RunsOnPhysicalHost);
  32. table.AddRow(
  33. s.Name,
  34. s.Ip ?? "",
  35. s.Port.ToString() ?? "",
  36. s.Protocol ?? "",
  37. s.Url ?? "",
  38. ServicesFormatExtensions.FormatRunsOn(sys, phys)
  39. );
  40. }
  41. AnsiConsole.Write(table);
  42. return 0;
  43. }
  44. }