BililiveRecorder/BililiveRecorder.ToolBox/ToolCommand.cs

79 lines
3.0 KiB
C#
Raw Normal View History

2021-04-14 23:46:24 +08:00
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Threading.Tasks;
using BililiveRecorder.ToolBox.Commands;
using Newtonsoft.Json;
namespace BililiveRecorder.ToolBox
{
public class ToolCommand : Command
{
public ToolCommand() : base("tool", "Run Tools")
{
this.RegisterCommand<AnalyzeHandler, AnalyzeRequest, AnalyzeResponse>("analyze", null, c =>
{
c.Add(new Argument<string>("input", "example: input.flv"));
});
this.RegisterCommand<FixHandler, FixRequest, FixResponse>("fix", null, c =>
{
c.Add(new Argument<string>("input", "example: input.flv"));
c.Add(new Argument<string>("output-base", "example: output.flv"));
});
this.RegisterCommand<ExportHandler, ExportRequest, ExportResponse>("export", null, c =>
{
c.Add(new Argument<string>("input", "example: input.flv"));
c.Add(new Argument<string>("output", "example: output.brec.xml.gz"));
});
}
2021-04-19 18:20:14 +08:00
private void RegisterCommand<THandler, TRequest, TResponse>(string name, string? description, Action<Command> configure)
where THandler : ICommandHandler<TRequest, TResponse>
where TRequest : ICommandRequest<TResponse>
where TResponse : class
2021-04-14 23:46:24 +08:00
{
var cmd = new Command(name, description)
{
new Option<bool>("--json", "print result as json string"),
new Option<bool>("--json-indented", "print result as indented json string")
};
2021-04-19 18:20:14 +08:00
cmd.Handler = CommandHandler.Create((TRequest r, bool json, bool jsonIndented) => RunSubCommand<THandler, TRequest, TResponse>(r, json, jsonIndented));
2021-04-14 23:46:24 +08:00
configure(cmd);
this.Add(cmd);
}
2021-04-19 18:20:14 +08:00
private static async Task<int> RunSubCommand<THandler, TRequest, TResponse>(TRequest request, bool json, bool jsonIndented)
where THandler : ICommandHandler<TRequest, TResponse>
where TRequest : ICommandRequest<TResponse>
where TResponse : class
2021-04-14 23:46:24 +08:00
{
2021-04-19 18:20:14 +08:00
var handler = Activator.CreateInstance<THandler>();
2021-04-14 23:46:24 +08:00
var response = await handler.Handle(request).ConfigureAwait(false);
if (json || jsonIndented)
{
var json_str = JsonConvert.SerializeObject(response, jsonIndented ? Formatting.Indented : Formatting.None);
Console.WriteLine(json_str);
}
else
{
2021-04-19 18:20:14 +08:00
if (response.Status == ResponseStatus.OK)
{
handler.PrintResponse(response.Result!);
}
else
{
Console.Write("Error: ");
Console.WriteLine(response.Status);
Console.WriteLine(response.ErrorMessage);
}
2021-04-14 23:46:24 +08:00
}
return 0;
}
}
}