LaptopCpuAddCommand.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 LaptopCpuAddSettings : CommandSettings
  9. {
  10. [CommandArgument(0, "<Laptop>")]
  11. [Description("The Laptop name.")]
  12. public string LaptopName { get; set; } = default!;
  13. [CommandOption("--model")]
  14. [Description("The model name.")]
  15. public string? Model { get; set; }
  16. [CommandOption("--cores")]
  17. [Description("The number of cpu cores.")]
  18. public int? Cores { get; set; }
  19. [CommandOption("--threads")]
  20. [Description("The number of cpu threads.")]
  21. public int? Threads { get; set; }
  22. }
  23. public class LaptopCpuAddCommand(IServiceProvider provider)
  24. : AsyncCommand<LaptopCpuAddSettings>
  25. {
  26. public override async Task<int> ExecuteAsync(
  27. CommandContext context,
  28. LaptopCpuAddSettings settings,
  29. CancellationToken cancellationToken)
  30. {
  31. using var scope = provider.CreateScope();
  32. var useCase = scope.ServiceProvider.GetRequiredService<IAddCpuUseCase<Laptop>>();
  33. await useCase.ExecuteAsync(settings.LaptopName, settings.Model, settings.Cores, settings.Threads);
  34. AnsiConsole.MarkupLine($"[green]CPU added to Laptop '{settings.LaptopName}'.[/]");
  35. return 0;
  36. }
  37. }