mirror of
https://github.com/duplicati/duplicati.git
synced 2025-11-28 03:20:25 +08:00
This comes from suggestion on issue https://github.com/duplicati/duplicati/issues/6056 The chosen argument was --json which produces a json that wraps the result with extended properties such as in example: `` { "Timestamp": "2025-03-28T15:47:01.3368160-03:00", "UnixTimestamp": 1743187621, "Command": "import", "Success": true, "ExitCode": 0, "Messages": [ "Importing backup configuration from ../2-firstbacku.json", "Connecting to http://127.0.0.1:8200/...", "No database found in../data/", "Imported \"firstbackup (5)\" with ID 8" ], "Exceptions": [], "Imported": { "Id": "8", "Name": "firstbackup (5)" } } `` The documentation will reflect all commands and schemas as this makes its way into Canary
62 lines
No EOL
2.8 KiB
C#
62 lines
No EOL
2.8 KiB
C#
using System.CommandLine.Binding;
|
|
using System.CommandLine.Parsing;
|
|
|
|
namespace Duplicati.CommandLine.ServerUtil;
|
|
|
|
/// <summary>
|
|
/// An abstract binder class for managing a singleton instance of <see cref="OutputInterceptor"/>.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This class ensures that only one instance of <see cref="OutputInterceptor"/> is associated with a given <see cref="BindingContext"/>.
|
|
/// </remarks>
|
|
public abstract class OutputInterceptorBinder : BinderBase<OutputInterceptor>
|
|
{
|
|
private static OutputInterceptor? _instance;
|
|
|
|
/// <summary>
|
|
/// Gets the current instance of <see cref="OutputInterceptor"/>.
|
|
/// </summary>
|
|
/// <exception cref="InvalidOperationException">Thrown when the instance has not been initialized.</exception>
|
|
public static OutputInterceptor? Instance => _instance;
|
|
|
|
/// <summary>
|
|
/// Retrieves or creates a <see cref="OutputInterceptor"/> instance for the specified binding context.
|
|
/// </summary>
|
|
/// <param name="bindingContext">The binding context to associate with the interceptor. Must not be null.</param>
|
|
/// <returns>The existing or newly created <see cref="OutputInterceptor"/> instance.</returns>
|
|
/// <exception cref="ArgumentNullException">Thrown when <paramref name="bindingContext"/> is null.</exception>
|
|
public static OutputInterceptor GetConsoleInterceptor(BindingContext bindingContext)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(bindingContext, nameof(bindingContext));
|
|
|
|
if (_instance is not null && ReferenceEquals(_instance.BindingContext, bindingContext))
|
|
{
|
|
return _instance;
|
|
}
|
|
|
|
_instance = CreateInterceptor(bindingContext);
|
|
return _instance;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the bound <see cref="OutputInterceptor"/> value for the specified binding context.
|
|
/// </summary>
|
|
/// <param name="bindingContext">The binding context to retrieve the interceptor for.</param>
|
|
/// <returns>The associated <see cref="OutputInterceptor"/> instance.</returns>
|
|
protected override OutputInterceptor GetBoundValue(BindingContext bindingContext)
|
|
{
|
|
return GetConsoleInterceptor(bindingContext);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new <see cref="OutputInterceptor"/> instance with the specified binding context.
|
|
/// </summary>
|
|
/// <param name="bindingContext">The binding context to initialize the interceptor with.</param>
|
|
/// <returns>A new <see cref="OutputInterceptor"/> instance.</returns>
|
|
private static OutputInterceptor CreateInterceptor(BindingContext bindingContext)
|
|
{
|
|
var interceptor = new OutputInterceptor(bindingContext.ParseResult.Tokens.Any(x => x is { Type: TokenType.Option, Value: "--json" }), bindingContext);
|
|
interceptor.SetCommand(bindingContext.ParseResult.CommandResult.Command.Name);
|
|
return interceptor;
|
|
}
|
|
} |