4
0

ServiceReportCommand.cs 1.8 KB

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