SystemSetCommand.cs 1.4 KB

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