4
0

ServiceSetCommand.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.ComponentModel;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using RackPeek.Domain.Resources.Services.UseCases;
  4. using Shared.Rcl.Commands.Servers;
  5. using Spectre.Console;
  6. using Spectre.Console.Cli;
  7. namespace Shared.Rcl.Commands.Services;
  8. public class ServiceSetSettings : ServerNameSettings {
  9. [CommandOption("--ip")]
  10. [Description("The ip address of the service.")]
  11. public string? Ip { get; set; }
  12. [CommandOption("--port")]
  13. [Description("The port the service is running on.")]
  14. public int? Port { get; set; }
  15. [CommandOption("--protocol")]
  16. [Description("The service protocol.")]
  17. public string? Protocol { get; set; }
  18. [CommandOption("--url")]
  19. [Description("The service URL.")]
  20. public string? Url { get; set; }
  21. [CommandOption("--runs-on <RUNSON>")]
  22. [Description("The system(s) the service is running on.")]
  23. public string[]? RunsOn { get; set; }
  24. }
  25. public class ServiceSetCommand(
  26. IServiceProvider serviceProvider
  27. ) : AsyncCommand<ServiceSetSettings> {
  28. public override async Task<int> ExecuteAsync(
  29. CommandContext context,
  30. ServiceSetSettings settings,
  31. CancellationToken cancellationToken) {
  32. using IServiceScope scope = serviceProvider.CreateScope();
  33. UpdateServiceUseCase useCase = scope.ServiceProvider.GetRequiredService<UpdateServiceUseCase>();
  34. await useCase.ExecuteAsync(
  35. settings.Name,
  36. settings.Ip,
  37. settings.Port,
  38. settings.Protocol,
  39. settings.Url,
  40. settings.RunsOn?.ToList()
  41. );
  42. AnsiConsole.MarkupLine($"[green]Service '{settings.Name}' updated.[/]");
  43. return 0;
  44. }
  45. }