DiscoverSettings.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.ComponentModel;
  2. using RackPeek.Domain.Discovery;
  3. using Spectre.Console;
  4. using Spectre.Console.Cli;
  5. namespace Shared.Rcl.Commands.Discovery;
  6. /// <summary>Output and upload options shared by every <c>rpk discover</c> command.</summary>
  7. public abstract class DiscoverSettings : CommandSettings {
  8. [CommandOption("--push")]
  9. [Description("Upload the result to a RackPeek server instead of printing it.")]
  10. public bool Push { get; init; }
  11. [CommandOption("--server <URL>")]
  12. [Description("RackPeek server to upload to. Defaults to the RPK_SERVER environment variable.")]
  13. public string? Server { get; init; }
  14. [CommandOption("--api-key <KEY>")]
  15. [Description("API key for the server. Defaults to the RPK_API_KEY environment variable.")]
  16. public string? ApiKey { get; init; }
  17. [CommandOption("--dry-run")]
  18. [Description("Ask the server what would change, without changing anything. Implies --push.")]
  19. public bool DryRun { get; init; }
  20. public bool ShouldUpload => Push || DryRun;
  21. public string? ResolvedServer => DiscoveryPublisher.ResolveServer(Server);
  22. public string? ResolvedApiKey => DiscoveryPublisher.ResolveApiKey(ApiKey);
  23. public override ValidationResult Validate() {
  24. if (!ShouldUpload)
  25. return ValidationResult.Success();
  26. if (string.IsNullOrWhiteSpace(ResolvedServer))
  27. return ValidationResult.Error(
  28. "No server to upload to. Pass --server or set RPK_SERVER.");
  29. if (string.IsNullOrWhiteSpace(ResolvedApiKey))
  30. return ValidationResult.Error(
  31. "No API key. Pass --api-key or set RPK_API_KEY.");
  32. return ValidationResult.Success();
  33. }
  34. }