LaptopCpuSetCommand.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.ComponentModel;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using RackPeek.Domain.Resources.Laptops;
  4. using RackPeek.Domain.UseCases.Cpus;
  5. using Spectre.Console;
  6. using Spectre.Console.Cli;
  7. namespace Shared.Rcl.Commands.Laptops.Cpus;
  8. public class LaptopCpuSetSettings : CommandSettings {
  9. [CommandArgument(0, "<Laptop>")]
  10. [Description("The Laptop name.")]
  11. public string LaptopName { get; set; } = default!;
  12. [CommandArgument(1, "<index>")]
  13. [Description("The index of the Laptop cpu.")]
  14. public int Index { get; set; }
  15. [CommandOption("--model")]
  16. [Description("The cpu model.")]
  17. public string? Model { get; set; }
  18. [CommandOption("--cores")]
  19. [Description("The number of cpu cores.")]
  20. public int? Cores { get; set; }
  21. [CommandOption("--threads")]
  22. [Description("The number of cpu threads.")]
  23. public int? Threads { get; set; }
  24. }
  25. public class LaptopCpuSetCommand(IServiceProvider provider)
  26. : AsyncCommand<LaptopCpuSetSettings> {
  27. public override async Task<int> ExecuteAsync(
  28. CommandContext context,
  29. LaptopCpuSetSettings settings,
  30. CancellationToken cancellationToken) {
  31. using IServiceScope scope = provider.CreateScope();
  32. IUpdateCpuUseCase<Laptop> useCase = scope.ServiceProvider.GetRequiredService<IUpdateCpuUseCase<Laptop>>();
  33. await useCase.ExecuteAsync(settings.LaptopName, settings.Index, settings.Model, settings.Cores,
  34. settings.Threads);
  35. AnsiConsole.MarkupLine($"[green]CPU #{settings.Index} updated on Laptop '{settings.LaptopName}'.[/]");
  36. return 0;
  37. }
  38. }