SwitchPortAddCommand.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System.ComponentModel;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using RackPeek.Domain.Resources.Switches;
  4. using RackPeek.Domain.UseCases.Ports;
  5. using Spectre.Console;
  6. using Spectre.Console.Cli;
  7. namespace Shared.Rcl.Commands.Switches.Ports;
  8. public class SwitchPortAddSettings : SwitchNameSettings {
  9. [CommandOption("--type")]
  10. [Description("The port type (e.g., rj45, sfp+).")]
  11. public string? Type { get; set; }
  12. [CommandOption("--speed")]
  13. [Description("The port speed (e.g., 1, 2.5, 10).")]
  14. public double? Speed { get; set; }
  15. [CommandOption("--count")]
  16. [Description("Number of ports of this type.")]
  17. public int? Count { get; set; }
  18. }
  19. public class SwitchPortAddCommand(IServiceProvider sp)
  20. : AsyncCommand<SwitchPortAddSettings> {
  21. public override async Task<int> ExecuteAsync(CommandContext ctx, SwitchPortAddSettings s, CancellationToken ct) {
  22. using IServiceScope scope = sp.CreateScope();
  23. IAddPortUseCase<Switch> useCase = scope.ServiceProvider.GetRequiredService<IAddPortUseCase<Switch>>();
  24. await useCase.ExecuteAsync(s.Name, s.Type, s.Speed, s.Count);
  25. AnsiConsole.MarkupLine($"[green]Port added to switch '{s.Name}'.[/]");
  26. return 0;
  27. }
  28. }