UpsGetCommand.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using Microsoft.Extensions.DependencyInjection;
  2. using RackPeek.Domain.Resources.UpsUnits;
  3. using Spectre.Console;
  4. using Spectre.Console.Cli;
  5. namespace Shared.Rcl.Commands.Ups;
  6. public class UpsGetCommand(IServiceProvider provider)
  7. : AsyncCommand {
  8. public override async Task<int> ExecuteAsync(
  9. CommandContext context,
  10. CancellationToken cancellationToken) {
  11. using IServiceScope scope = provider.CreateScope();
  12. UpsHardwareReportUseCase useCase = scope.ServiceProvider.GetRequiredService<UpsHardwareReportUseCase>();
  13. UpsHardwareReport report = await useCase.ExecuteAsync();
  14. if (report.UpsUnits.Count == 0) {
  15. AnsiConsole.MarkupLine("[yellow]No UPS units found.[/]");
  16. return 0;
  17. }
  18. Table table = new Table()
  19. .Border(TableBorder.Rounded)
  20. .AddColumn("Name")
  21. .AddColumn("Model")
  22. .AddColumn("VA");
  23. foreach (UpsHardwareRow ups in report.UpsUnits)
  24. table.AddRow(
  25. ups.Name,
  26. ups.Model,
  27. ups.Va.ToString()
  28. );
  29. AnsiConsole.Write(table);
  30. return 0;
  31. }
  32. }