LaptopCpuAddCommand.cs 1.4 KB

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