UpsPortUpdateCommand.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.ComponentModel;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using RackPeek.Domain.UseCases.Ports;
  4. using Spectre.Console;
  5. using Spectre.Console.Cli;
  6. using UpsUnit = RackPeek.Domain.Resources.UpsUnits.Ups;
  7. namespace Shared.Rcl.Commands.Ups.Ports;
  8. public class UpsPortUpdateSettings : UpsNameSettings {
  9. [CommandOption("--index <INDEX>")]
  10. [Description("The index of the port to update.")]
  11. public int Index { get; set; }
  12. [CommandOption("--type")]
  13. [Description("The port type (e.g., rj45, usb).")]
  14. public string? Type { get; set; }
  15. [CommandOption("--speed")]
  16. [Description("The port speed (e.g., 0.1, 1).")]
  17. public double? Speed { get; set; }
  18. [CommandOption("--count")]
  19. [Description("Number of ports of this type.")]
  20. public int? Count { get; set; }
  21. }
  22. public class UpsPortUpdateCommand(IServiceProvider sp)
  23. : AsyncCommand<UpsPortUpdateSettings> {
  24. protected override async Task<int> ExecuteAsync(CommandContext ctx, UpsPortUpdateSettings s, CancellationToken ct) {
  25. using IServiceScope scope = sp.CreateScope();
  26. IUpdatePortUseCase<UpsUnit> useCase = scope.ServiceProvider.GetRequiredService<IUpdatePortUseCase<UpsUnit>>();
  27. await useCase.ExecuteAsync(s.Name, s.Index, s.Type, s.Speed, s.Count);
  28. AnsiConsole.MarkupLine($"[green]Port #{s.Index} updated on UPS '{s.Name}'.[/]");
  29. return 0;
  30. }
  31. }