ServiceGetCommand.cs 1.8 KB

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