-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adds output formatters - Adds output formatters
- Loading branch information
1 parent
5f15347
commit e9d74b1
Showing
44 changed files
with
1,712 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/Confix.Tool/src/Confix.Library/Common/ContextData/Context.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Confix.Tool; | ||
|
||
public static class Context | ||
{ | ||
public static Key<bool> DisableStatus { get; } = new("Confix.Tool.Settings.DisableStatus"); | ||
|
||
public static Key<object> Output { get; } = new("Confix.Tool.Output"); | ||
|
||
public readonly record struct Key<T>(string Id); | ||
} |
64 changes: 64 additions & 0 deletions
64
...fix.Tool/src/Confix.Library/Common/ContextData/ContextDataCommandLineBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.CommandLine.Builder; | ||
using System.CommandLine.Invocation; | ||
using System.Runtime.CompilerServices; | ||
using Confix.Extensions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Confix.Tool; | ||
|
||
public static class ContextDataCommandLineBuilderExtensions | ||
{ | ||
private static readonly ConditionalWeakTable<CommandLineBuilder, ContextData> _contextData = | ||
new(); | ||
|
||
public static CommandLineBuilder AddContextData(this CommandLineBuilder builder) | ||
{ | ||
var contextData = new ContextData(); | ||
builder.AddSingleton(contextData); | ||
_contextData.Add(builder, contextData); | ||
|
||
return builder; | ||
} | ||
|
||
public static CommandLineBuilder SetContextData<T>( | ||
this CommandLineBuilder builder, | ||
Context.Key<T> key, | ||
T value) | ||
where T : notnull | ||
{ | ||
builder.AddMiddleware((context, next) => | ||
{ | ||
var data = context.BindingContext.GetRequiredService<ContextData>(); | ||
data.Data.Set(key, value); | ||
return next(context); | ||
}); | ||
|
||
return builder; | ||
} | ||
|
||
public static IDictionary<string, object> GetContextData(this InvocationContext context) | ||
{ | ||
return context.BindingContext.GetRequiredService<ContextData>().Data; | ||
} | ||
|
||
public static IDictionary<string, object> GetContextData(this CommandLineBuilder builder) | ||
{ | ||
return _contextData.GetOrCreateValue(builder).Data; | ||
} | ||
|
||
public static void SetContextData<T>( | ||
this InvocationContext context, | ||
in Context.Key<T> key, | ||
T value) | ||
where T : notnull | ||
{ | ||
var data = context.BindingContext.GetRequiredService<ContextData>(); | ||
data.Data.Set(key, value); | ||
} | ||
|
||
private class ContextData | ||
{ | ||
public IDictionary<string, object> Data { get; } = new Dictionary<string, object>(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Confix.Tool/src/Confix.Library/Common/ContextData/ContextDataExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Confix.Tool; | ||
|
||
public static class ContextDataExtensions | ||
{ | ||
public static T? Get<T>(this IDictionary<string, object> contextData, in Context.Key<T> key) | ||
{ | ||
if (contextData.TryGetValue(key.Id, out var value)) | ||
{ | ||
return (T?) value; | ||
} | ||
|
||
return default; | ||
} | ||
|
||
public static void Set<T>( | ||
this IDictionary<string, object> contextData, | ||
in Context.Key<T> key, | ||
T value) | ||
where T : notnull | ||
{ | ||
contextData[key.Id] = value; | ||
} | ||
|
||
public static bool TryGetValue<T>( | ||
this IDictionary<string, object> contextData, | ||
in Context.Key<T> key, | ||
[NotNullWhen(true)] out T? value) | ||
{ | ||
if (contextData.TryGetValue(key.Id, out var v)) | ||
{ | ||
value = (T) v; | ||
return true; | ||
} | ||
|
||
value = default; | ||
return false; | ||
} | ||
|
||
public static T GetOrAddValue<T>( | ||
this IDictionary<string, object> contextData, | ||
in Context.Key<T> key) where T : new() | ||
{ | ||
if (!contextData.TryGetValue(key.Id, out var v) || v is not T value) | ||
{ | ||
value = new T(); | ||
contextData[key.Id] = value; | ||
} | ||
|
||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,4 @@ public static CommandLineBuilder UseVerbosity(this CommandLineBuilder builder) | |
|
||
return builder; | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Confix.Tool/src/Confix.Library/Common/Output/IOutputFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.CommandLine.Invocation; | ||
|
||
namespace Confix.Tool.Commands.Logging; | ||
|
||
public interface IOutputFormatter | ||
{ | ||
bool CanHandle(OutputFormat format, object value); | ||
|
||
Task<string> FormatAsync(InvocationContext context, OutputFormat format, object value); | ||
} | ||
|
||
public interface IOutputFormatter<in T> : IOutputFormatter | ||
{ | ||
bool IOutputFormatter.CanHandle(OutputFormat format, object value) | ||
=> value is T t && CanHandle(format, t); | ||
|
||
bool CanHandle(OutputFormat format, T value); | ||
|
||
Task<string> IOutputFormatter.FormatAsync( | ||
InvocationContext context, | ||
OutputFormat format, | ||
object value) | ||
{ | ||
if (value is T t) | ||
{ | ||
return FormatAsync(context, format, t); | ||
} | ||
|
||
throw new ArgumentException($"Expected {typeof(T).Name} but got {value.GetType().Name}"); | ||
} | ||
|
||
Task<string> FormatAsync(InvocationContext context, OutputFormat format, T value); | ||
} |
74 changes: 74 additions & 0 deletions
74
src/Confix.Tool/src/Confix.Library/Common/Output/OutputFormatCommandLineBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System.CommandLine.Builder; | ||
using Spectre.Console; | ||
|
||
namespace Confix.Tool.Commands.Logging; | ||
|
||
public static class OutputFormatCommandLineBuilderExtensions | ||
{ | ||
private static Context.Key<List<IOutputFormatter>> _key = | ||
new("Confix.Tool.Common.OutputFormatter"); | ||
|
||
public static CommandLineBuilder AddOutputFormatter<T>(this CommandLineBuilder builder) | ||
where T : IOutputFormatter, new() | ||
{ | ||
builder.GetOutputFormatters().Add(new T()); | ||
return builder; | ||
} | ||
|
||
public static CommandLineBuilder UseOutputFormat(this CommandLineBuilder builder) | ||
{ | ||
builder.AddMiddleware(async (context, next) => | ||
{ | ||
var format = | ||
context.ParseResult.GetValueForOption(FormatOption.Instance) ?? | ||
context.ParseResult.GetValueForOption(FormatOptionWithDefault.Instance); | ||
if (format is not null) | ||
{ | ||
context.SetContextData(Context.DisableStatus, true); | ||
// we disable logging if the format option is specified | ||
using (App.Log.SetVerbosity(Verbosity.Quiet)) | ||
{ | ||
await next(context); | ||
} | ||
var outputFormatters = builder.GetOutputFormatters(); | ||
if (builder.GetOutput() is { } output) | ||
{ | ||
string? formattedValue = null; | ||
foreach (var outputFormatter in outputFormatters) | ||
{ | ||
if (!outputFormatter.CanHandle(format.Value, output)) | ||
{ | ||
continue; | ||
} | ||
formattedValue = | ||
await outputFormatter.FormatAsync(context, format.Value, output); | ||
break; | ||
} | ||
context.Console.Out.Write(formattedValue); | ||
} | ||
} | ||
else | ||
{ | ||
await next(context); | ||
} | ||
}); | ||
|
||
return builder; | ||
} | ||
|
||
private static List<IOutputFormatter> GetOutputFormatters(this CommandLineBuilder builder) | ||
=> builder.GetContextData().GetOrAddValue(_key); | ||
|
||
private static object? GetOutput(this CommandLineBuilder context) | ||
{ | ||
return context.GetContextData().Get(Context.Output); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Confix.Tool/src/Confix.Library/Common/Output/OutputMiddlewareContextExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Confix.Tool.Common.Pipelines; | ||
|
||
namespace Confix.Tool.Commands.Logging; | ||
|
||
public static class OutputMiddlewareContextExtensions | ||
{ | ||
public static void SetOutput(this IMiddlewareContext context, object? result) | ||
{ | ||
if (result is null) | ||
{ | ||
return; | ||
} | ||
|
||
context.ContextData.Set(Context.Output, result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.