GraphTopologyCommand.cs 1.0 KB

12345678910111213141516171819202122232425
  1. using Microsoft.Extensions.DependencyInjection;
  2. using RackPeek.Domain.Graph.Serialisers;
  3. using RackPeek.Domain.Graph.UseCases;
  4. using Spectre.Console.Cli;
  5. namespace Shared.Rcl.Commands.Graph;
  6. public class GraphTopologyCommand(IServiceProvider serviceProvider) : AsyncCommand {
  7. protected override async Task<int> ExecuteAsync(
  8. CommandContext context,
  9. CancellationToken cancellationToken) {
  10. using IServiceScope scope = serviceProvider.CreateScope();
  11. BuildPhysicalTopologyUseCase useCase =
  12. scope.ServiceProvider.GetRequiredService<BuildPhysicalTopologyUseCase>();
  13. RackPeek.Domain.Graph.Graph graph = await useCase.ExecuteAsync();
  14. var mermaid = new MermaidSerialiser().Serialise(graph);
  15. // Use Console.Out directly rather than AnsiConsole — Spectre soft-wraps
  16. // long lines based on terminal width, which would corrupt the Mermaid
  17. // syntax when the user pipes the output to a file or another tool.
  18. System.Console.Out.Write(mermaid);
  19. return 0;
  20. }
  21. }