ServiceSetCommand.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // TODO: How do you specify a list?
  23. // foo --runs-on a --runs-on b
  24. // foo --runs-on a,b
  25. [CommandOption("--runs-on")]
  26. [Description("The system the service is running on.")]
  27. public List<string>? RunsOn { get; set; }
  28. }
  29. public class ServiceSetCommand(
  30. IServiceProvider serviceProvider
  31. ) : AsyncCommand<ServiceSetSettings>
  32. {
  33. public override async Task<int> ExecuteAsync(
  34. CommandContext context,
  35. ServiceSetSettings settings,
  36. CancellationToken cancellationToken)
  37. {
  38. using var scope = serviceProvider.CreateScope();
  39. var useCase = scope.ServiceProvider.GetRequiredService<UpdateServiceUseCase>();
  40. await useCase.ExecuteAsync(
  41. settings.Name,
  42. settings.Ip,
  43. settings.Port,
  44. settings.Protocol,
  45. settings.Url,
  46. settings.RunsOn
  47. );
  48. AnsiConsole.MarkupLine($"[green]Service '{settings.Name}' updated.[/]");
  49. return 0;
  50. }
  51. }