ServiceReportCommand.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 ServiceReportCommand(
  7. IServiceProvider serviceProvider
  8. ) : AsyncCommand {
  9. public override async Task<int> ExecuteAsync(CommandContext context, CancellationToken cancellationToken) {
  10. using IServiceScope scope = serviceProvider.CreateScope();
  11. ServiceReportUseCase useCase = scope.ServiceProvider.GetRequiredService<ServiceReportUseCase>();
  12. ServiceReport report = await useCase.ExecuteAsync();
  13. if (report.Services.Count == 0) {
  14. AnsiConsole.MarkupLine("[yellow]No Services found.[/]");
  15. return 0;
  16. }
  17. Table table = new Table()
  18. .Border(TableBorder.Rounded)
  19. .AddColumn("Name")
  20. .AddColumn("Ip")
  21. .AddColumn("Port")
  22. .AddColumn("Protocol")
  23. .AddColumn("Url")
  24. .AddColumn("Runs On");
  25. foreach (ServiceReportRow s in report.Services) {
  26. string? sys = null;
  27. string? phys = null;
  28. if (s.RunsOnSystemHost?.Count > 0) sys = string.Join(", ", s.RunsOnSystemHost);
  29. if (s.RunsOnPhysicalHost?.Count > 0) phys = string.Join(", ", s.RunsOnPhysicalHost);
  30. table.AddRow(
  31. s.Name,
  32. s.Ip ?? "",
  33. s.Port.ToString() ?? "",
  34. s.Protocol ?? "",
  35. s.Url ?? "",
  36. ServicesFormatExtensions.FormatRunsOn(sys, phys)
  37. );
  38. }
  39. AnsiConsole.Write(table);
  40. return 0;
  41. }
  42. }