SystemSetCommand.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. [CommandOption("--ip")]
  18. [Description("The ip address of the system.")]
  19. public string? Ip { get; set; }
  20. }
  21. public class SystemSetCommand(
  22. IServiceProvider serviceProvider
  23. ) : AsyncCommand<SystemSetSettings>
  24. {
  25. public override async Task<int> ExecuteAsync(
  26. CommandContext context,
  27. SystemSetSettings settings,
  28. CancellationToken cancellationToken)
  29. {
  30. using var scope = serviceProvider.CreateScope();
  31. var useCase = scope.ServiceProvider.GetRequiredService<UpdateSystemUseCase>();
  32. await useCase.ExecuteAsync(
  33. settings.Name,
  34. settings.Type,
  35. settings.Os,
  36. settings.Cores,
  37. settings.Ram,
  38. settings.Ip,
  39. settings.RunsOn?.ToList()
  40. );
  41. AnsiConsole.MarkupLine($"[green]System '{settings.Name}' updated.[/]");
  42. return 0;
  43. }
  44. }