Skip to content

Commit

Permalink
Adds json formatter to variable get (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Mar 18, 2024
1 parent 3584891 commit 09f43d8
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.CommandLine.Builder;
using System.CommandLine.IO;
using Spectre.Console;

namespace Confix.Tool.Commands.Logging;
Expand Down Expand Up @@ -55,6 +56,7 @@ public static CommandLineBuilder UseOutputFormat(this CommandLineBuilder builder
}
context.Console.Out.Write(formattedValue);
context.Console.Out.WriteLine();
}
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text.Json.Nodes;
using Confix.Tool.Commands.Logging;
using Confix.Tool.Reporting;

namespace Confix.Tool.Commands.Configuration;

public sealed class JsonNodeOutputFormatter : IOutputFormatter<JsonNode>
{
/// <inheritdoc />
public bool CanHandle(OutputFormat format, JsonNode value)
=> format == OutputFormat.Json;

/// <inheritdoc />
public Task<string> FormatAsync(OutputFormat format, JsonNode value)
{
return Task.FromResult(value.ToJsonString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static CommandLineBuilder AddOutputFormatters(this CommandLineBuilder bui
builder.AddOutputFormatter<ConfigurationFeatureOutputFormatter>();
builder.AddOutputFormatter<ConfigurationFileOutputFormatter>();
builder.AddOutputFormatter<ReportListOutputFormatter>();
builder.AddOutputFormatter<JsonNodeOutputFormatter>();
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ protected override void Configure(IPipelineDescriptor builder)
.UseEnvironment()
.Use<VariableMiddleware>()
.AddOption(VariableNameOption.Instance)
.AddOption(FormatOption.Instance)
.UseHandler(InvokeAsync);
}

Expand All @@ -34,6 +35,8 @@ private static async Task InvokeAsync(IMiddlewareContext context)
.ResolveOrThrowAsync(variablePath, context.CancellationToken);

context.Logger.PrintVariableResolved(variablePath, result.ToJsonString());

context.SetOutput(result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@
}
]
}

Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@
}
]
}

Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@
}
}
}

This file was deleted.

This file was deleted.

0 comments on commit 09f43d8

Please sign in to comment.