SwitchLabelAddCommand.cs 1.1 KB

1234567891011121314151617181920212223
  1. using Microsoft.Extensions.DependencyInjection;
  2. using RackPeek.Domain.Resources.Switches;
  3. using RackPeek.Domain.UseCases.Labels;
  4. using Spectre.Console;
  5. using Spectre.Console.Cli;
  6. namespace Shared.Rcl.Commands.Switches.Labels;
  7. public class SwitchLabelAddSettings : SwitchNameSettings {
  8. [CommandOption("--key <KEY>")] public string Key { get; set; } = default!;
  9. [CommandOption("--value <VALUE>")] public string Value { get; set; } = default!;
  10. }
  11. public class SwitchLabelAddCommand(IServiceProvider serviceProvider) : AsyncCommand<SwitchLabelAddSettings> {
  12. public override async Task<int> ExecuteAsync(CommandContext context, SwitchLabelAddSettings settings,
  13. CancellationToken cancellationToken) {
  14. using IServiceScope scope = serviceProvider.CreateScope();
  15. IAddLabelUseCase<Switch> useCase = scope.ServiceProvider.GetRequiredService<IAddLabelUseCase<Switch>>();
  16. await useCase.ExecuteAsync(settings.Name, settings.Key, settings.Value);
  17. AnsiConsole.MarkupLine($"[green]Label '{settings.Key}' added to '{settings.Name}'.[/]");
  18. return 0;
  19. }
  20. }