ServiceSetCommand.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. {
  10. [CommandOption("--ip")]
  11. [Description("The ip address of the service.")]
  12. public string? Ip { get; set; }
  13. [CommandOption("--port")]
  14. [Description("The port the service is running on.")]
  15. public int? Port { get; set; }
  16. [CommandOption("--protocol")]
  17. [Description("The service protocol.")]
  18. public string? Protocol { get; set; }
  19. [CommandOption("--url")]
  20. [Description("The service URL.")]
  21. public string? Url { get; set; }
  22. [CommandOption("--runs-on")]
  23. [Description("The system the service is running on.")]
  24. public string? RunsOn { get; set; }
  25. }
  26. public class ServiceSetCommand(
  27. IServiceProvider serviceProvider
  28. ) : AsyncCommand<ServiceSetSettings>
  29. {
  30. public override async Task<int> ExecuteAsync(
  31. CommandContext context,
  32. ServiceSetSettings settings,
  33. CancellationToken cancellationToken)
  34. {
  35. using var scope = serviceProvider.CreateScope();
  36. var useCase = scope.ServiceProvider.GetRequiredService<UpdateServiceUseCase>();
  37. await useCase.ExecuteAsync(
  38. settings.Name,
  39. settings.Ip,
  40. settings.Port,
  41. settings.Protocol,
  42. settings.Url,
  43. settings.RunsOn
  44. );
  45. AnsiConsole.MarkupLine($"[green]Service '{settings.Name}' updated.[/]");
  46. return 0;
  47. }
  48. }