SystemGetCommand.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Microsoft.Extensions.DependencyInjection;
  2. using RackPeek.Domain.Resources.SystemResources.UseCases;
  3. using Spectre.Console;
  4. using Spectre.Console.Cli;
  5. namespace Shared.Rcl.Commands.Systems;
  6. public class SystemGetCommand(
  7. IServiceProvider serviceProvider
  8. ) : AsyncCommand {
  9. public override async Task<int> ExecuteAsync(
  10. CommandContext context,
  11. CancellationToken cancellationToken) {
  12. using IServiceScope scope = serviceProvider.CreateScope();
  13. SystemReportUseCase useCase = scope.ServiceProvider.GetRequiredService<SystemReportUseCase>();
  14. SystemReport report = await useCase.ExecuteAsync();
  15. if (report.Systems.Count == 0) {
  16. AnsiConsole.MarkupLine("[yellow]No systems found.[/]");
  17. return 0;
  18. }
  19. Table table = new Table()
  20. .Border(TableBorder.Rounded)
  21. .AddColumn("Name")
  22. .AddColumn("Type")
  23. .AddColumn("OS")
  24. .AddColumn("Cores")
  25. .AddColumn("RAM (GB)")
  26. .AddColumn("Storage (GB)")
  27. .AddColumn("Runs On");
  28. foreach (SystemReportRow s in report.Systems)
  29. table.AddRow(
  30. s.Name,
  31. s.Type ?? "Unknown",
  32. s.Os ?? "Unknown",
  33. s.Cores.ToString(),
  34. s.RamGb.ToString(),
  35. s.TotalStorageGb.ToString(),
  36. string.Join(", ", s.RunsOn) ?? "Unkown"
  37. );
  38. AnsiConsole.Write(table);
  39. return 0;
  40. }
  41. }