SwitchPortAddCommand.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. {
  10. [CommandOption("--type")]
  11. [Description("The port type (e.g., rj45, sfp+).")]
  12. public string? Type { get; set; }
  13. [CommandOption("--speed")]
  14. [Description("The port speed (e.g., 1, 2.5, 10).")]
  15. public double? Speed { get; set; }
  16. [CommandOption("--count")]
  17. [Description("Number of ports of this type.")]
  18. public int? Count { get; set; }
  19. }
  20. public class SwitchPortAddCommand(IServiceProvider sp)
  21. : AsyncCommand<SwitchPortAddSettings>
  22. {
  23. public override async Task<int> ExecuteAsync(CommandContext ctx, SwitchPortAddSettings s, CancellationToken ct)
  24. {
  25. using var scope = sp.CreateScope();
  26. var useCase = scope.ServiceProvider.GetRequiredService<IAddPortUseCase<Switch>>();
  27. await useCase.ExecuteAsync(s.Name, s.Type, s.Speed, s.Count);
  28. AnsiConsole.MarkupLine($"[green]Port added to switch '{s.Name}'.[/]");
  29. return 0;
  30. }
  31. }