DiscoverSystemCommand.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System.ComponentModel;
  2. using RackPeek.Domain.Discovery;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.SystemResources;
  5. using Spectre.Console;
  6. using Spectre.Console.Cli;
  7. namespace Shared.Rcl.Commands.Discovery;
  8. public sealed class DiscoverSystemSettings : DiscoverSettings {
  9. [CommandOption("-n|--name <NAME>")]
  10. [Description("Name for this machine. Defaults to its hostname. Recommended when running from a timer.")]
  11. public string? Name { get; init; }
  12. }
  13. /// <summary>Inspects the machine it is running on and emits it as a System resource.</summary>
  14. public sealed class DiscoverSystemCommand(IEnumerable<ISystemProbe> probes)
  15. : AsyncCommand<DiscoverSystemSettings> {
  16. protected override async Task<int> ExecuteAsync(
  17. CommandContext context,
  18. DiscoverSystemSettings settings,
  19. CancellationToken cancellationToken) {
  20. SystemFacts? facts = await SystemProbes.TryReadHostAsync(probes, cancellationToken);
  21. if (facts == null) {
  22. AnsiConsole.MarkupLine(
  23. "[red]No probe for this platform.[/] System discovery currently supports Linux and macOS.");
  24. return 1;
  25. }
  26. if (facts.MachineId == null)
  27. AnsiConsole.MarkupLine(
  28. "[yellow]Warning:[/] no machine id available, so the hostname is being used as this " +
  29. "machine's identity. Renaming the host will look like a new machine.");
  30. SystemResource resource = SystemResourceMapper.ToResource(facts, settings.Name);
  31. return await DiscoveryOutput.EmitAsync([resource], settings, cancellationToken);
  32. }
  33. }