|
|
@@ -6,7 +6,8 @@ using Spectre.Console.Cli;
|
|
|
|
|
|
namespace Shared.Rcl.Commands.Exporters;
|
|
|
|
|
|
-public sealed class GenerateAnsibleInventorySettings : CommandSettings {
|
|
|
+public sealed class GenerateAnsibleInventorySettings : CommandSettings
|
|
|
+{
|
|
|
[CommandOption("--group-tags")]
|
|
|
[Description("Comma-separated list of tags to group by (e.g. prod,staging)")]
|
|
|
public string? GroupTags { get; init; }
|
|
|
@@ -30,23 +31,27 @@ public sealed class GenerateAnsibleInventorySettings : CommandSettings {
|
|
|
}
|
|
|
|
|
|
public sealed class GenerateAnsibleInventoryCommand(IServiceProvider provider)
|
|
|
- : AsyncCommand<GenerateAnsibleInventorySettings> {
|
|
|
+ : AsyncCommand<GenerateAnsibleInventorySettings>
|
|
|
+{
|
|
|
public override async Task<int> ExecuteAsync(
|
|
|
CommandContext context,
|
|
|
GenerateAnsibleInventorySettings settings,
|
|
|
- CancellationToken cancellationToken) {
|
|
|
+ CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
using IServiceScope scope = provider.CreateScope();
|
|
|
|
|
|
AnsibleInventoryGeneratorUseCase useCase = scope.ServiceProvider
|
|
|
.GetRequiredService<AnsibleInventoryGeneratorUseCase>();
|
|
|
|
|
|
- if (!TryParseFormat(settings.Format, out InventoryFormat format)) {
|
|
|
+ if (!TryParseFormat(settings.Format, out InventoryFormat format))
|
|
|
+ {
|
|
|
AnsiConsole.MarkupLine(
|
|
|
$"[red]Invalid format:[/] {Markup.Escape(settings.Format)}. Use 'ini' or 'yaml'.");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- var options = new InventoryOptions {
|
|
|
+ var options = new InventoryOptions
|
|
|
+ {
|
|
|
Format = format,
|
|
|
GroupByTags = ParseCsv(settings.GroupTags),
|
|
|
GroupByLabelKeys = ParseCsv(settings.GroupLabels),
|
|
|
@@ -55,19 +60,22 @@ public sealed class GenerateAnsibleInventoryCommand(IServiceProvider provider)
|
|
|
|
|
|
InventoryResult? result = await useCase.ExecuteAsync(options);
|
|
|
|
|
|
- if (result is null) {
|
|
|
+ if (result is null)
|
|
|
+ {
|
|
|
AnsiConsole.MarkupLine("[red]Inventory generation returned null.[/]");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- if (result.Warnings.Any()) {
|
|
|
+ if (result.Warnings.Any())
|
|
|
+ {
|
|
|
AnsiConsole.MarkupLine("[yellow]Warnings:[/]");
|
|
|
foreach (var warning in result.Warnings)
|
|
|
AnsiConsole.MarkupLine($"[yellow]- {Markup.Escape(warning)}[/]");
|
|
|
AnsiConsole.WriteLine();
|
|
|
}
|
|
|
|
|
|
- if (!string.IsNullOrWhiteSpace(settings.OutputPath)) {
|
|
|
+ if (!string.IsNullOrWhiteSpace(settings.OutputPath))
|
|
|
+ {
|
|
|
await File.WriteAllTextAsync(
|
|
|
settings.OutputPath,
|
|
|
result.InventoryText,
|
|
|
@@ -76,7 +84,8 @@ public sealed class GenerateAnsibleInventoryCommand(IServiceProvider provider)
|
|
|
AnsiConsole.MarkupLine(
|
|
|
$"[green]Inventory written to:[/] {Markup.Escape(settings.OutputPath)}");
|
|
|
}
|
|
|
- else {
|
|
|
+ else
|
|
|
+ {
|
|
|
AnsiConsole.MarkupLine("[green]Generated Inventory:[/]");
|
|
|
AnsiConsole.WriteLine();
|
|
|
AnsiConsole.Write(result.InventoryText);
|
|
|
@@ -87,8 +96,10 @@ public sealed class GenerateAnsibleInventoryCommand(IServiceProvider provider)
|
|
|
|
|
|
// ------------------------
|
|
|
|
|
|
- private static bool TryParseFormat(string raw, out InventoryFormat format) {
|
|
|
- format = raw.Trim().ToLowerInvariant() switch {
|
|
|
+ private static bool TryParseFormat(string raw, out InventoryFormat format)
|
|
|
+ {
|
|
|
+ format = raw.Trim().ToLowerInvariant() switch
|
|
|
+ {
|
|
|
"ini" => InventoryFormat.Ini,
|
|
|
"yaml" => InventoryFormat.Yaml,
|
|
|
"yml" => InventoryFormat.Yaml,
|
|
|
@@ -100,7 +111,8 @@ public sealed class GenerateAnsibleInventoryCommand(IServiceProvider provider)
|
|
|
|| raw.Equals("yml", StringComparison.OrdinalIgnoreCase);
|
|
|
}
|
|
|
|
|
|
- private static IReadOnlyList<string> ParseCsv(string? raw) {
|
|
|
+ private static IReadOnlyList<string> ParseCsv(string? raw)
|
|
|
+ {
|
|
|
if (string.IsNullOrWhiteSpace(raw))
|
|
|
return [];
|
|
|
|
|
|
@@ -110,10 +122,12 @@ public sealed class GenerateAnsibleInventoryCommand(IServiceProvider provider)
|
|
|
.ToArray();
|
|
|
}
|
|
|
|
|
|
- private static IDictionary<string, string> ParseGlobalVars(string[] vars) {
|
|
|
+ private static IDictionary<string, string> ParseGlobalVars(string[] vars)
|
|
|
+ {
|
|
|
var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
- foreach (var entry in vars ?? []) {
|
|
|
+ foreach (var entry in vars ?? [])
|
|
|
+ {
|
|
|
var parts = entry.Split('=', 2);
|
|
|
if (parts.Length != 2)
|
|
|
continue;
|