SystemSetCommand.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Microsoft.Extensions.DependencyInjection;
  2. using RackPeek.Domain.Resources.SystemResources.UseCases;
  3. using Shared.Rcl.Commands.Servers;
  4. using Spectre.Console;
  5. using Spectre.Console.Cli;
  6. namespace Shared.Rcl.Commands.Systems;
  7. public class SystemSetSettings : ServerNameSettings
  8. {
  9. [CommandOption("--type")] public string? Type { get; set; }
  10. [CommandOption("--os")] public string? Os { get; set; }
  11. [CommandOption("--cores")] public int? Cores { get; set; }
  12. [CommandOption("--ram")] public int? Ram { get; set; }
  13. [CommandOption("--runs-on")] public string? RunsOn { get; set; }
  14. }
  15. public class SystemSetCommand(
  16. IServiceProvider serviceProvider
  17. ) : AsyncCommand<SystemSetSettings>
  18. {
  19. public override async Task<int> ExecuteAsync(
  20. CommandContext context,
  21. SystemSetSettings settings,
  22. CancellationToken cancellationToken)
  23. {
  24. using var scope = serviceProvider.CreateScope();
  25. var useCase = scope.ServiceProvider.GetRequiredService<UpdateSystemUseCase>();
  26. List<string> runsOn = new List<string>();
  27. if (settings.RunsOn is not null)
  28. {
  29. runsOn.Add(settings.RunsOn);
  30. }
  31. await useCase.ExecuteAsync(
  32. settings.Name,
  33. settings.Type,
  34. settings.Os,
  35. settings.Cores,
  36. settings.Ram,
  37. runsOn
  38. );
  39. AnsiConsole.MarkupLine($"[green]System '{settings.Name}' updated.[/]");
  40. return 0;
  41. }
  42. }