mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
AppTests
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using System.Reflection;
|
||||
using Govor.ConsoleClient.Commands;
|
||||
using Govor.ConsoleClient.Services.Interfaces;
|
||||
|
||||
namespace Govor.ConsoleClient.Services.Implementations;
|
||||
|
||||
public class CommandDispatcher : ICommandDispatcher
|
||||
{
|
||||
private readonly Dictionary<string, ICommand> _commands = new();
|
||||
private readonly ILogger _logger;
|
||||
private readonly IMiddlewarePipeline _pipeline;
|
||||
|
||||
public CommandDispatcher(IEnumerable<ICommand> commands, ILogger logger, IMiddlewarePipeline pipeline)
|
||||
{
|
||||
_logger = logger;
|
||||
_pipeline = pipeline;
|
||||
|
||||
foreach (var command in commands)
|
||||
{
|
||||
var route = command.GetType().GetCustomAttribute<CommandRouteAttribute>()?.Path.Replace("/","")
|
||||
?? command.GetType().Name.Replace("Command", "").ToLower();
|
||||
_commands[route.ToLower()] = command;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICommand?> DispatchAsync(string input)
|
||||
{
|
||||
var args = input.Split(' ', 2);
|
||||
var cmd = args[0].ToLower();
|
||||
if (_commands.TryGetValue(cmd, out var command))
|
||||
{
|
||||
var context = new CommandContext(cmd, args.Length > 1 ? args[1] : null, command);
|
||||
await _pipeline.ExecuteAsync(context);
|
||||
return command;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Warn("Неизвестная команда. Введите '/help'.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<ICommand> GetAllCommands() => _commands.Values;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using Govor.ConsoleClient.Services.Interfaces;
|
||||
|
||||
namespace Govor.ConsoleClient.Services.Implementations;
|
||||
|
||||
public class ConsoleLogger : ILogger
|
||||
{
|
||||
public void Log(string message)
|
||||
{
|
||||
Console.ResetColor();
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
|
||||
public void Info(string message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"[INFO] {message}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
public void Warn(string message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
Console.WriteLine($"[WARN] {message}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
public void Error(string message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"[ERROR] {message}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
public void Title(string message)
|
||||
{
|
||||
var upper = message.ToUpper();
|
||||
var length = upper.Length + 6;
|
||||
var border = new string('=', length);
|
||||
var padded = $"= {upper} =";
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
Console.WriteLine(border);
|
||||
Console.WriteLine(padded);
|
||||
Console.WriteLine(border);
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using Govor.ConsoleClient.Commands;
|
||||
using Govor.ConsoleClient.Services.Interfaces;
|
||||
|
||||
namespace Govor.ConsoleClient.Services.Implementations;
|
||||
|
||||
public class InputPipeline : IInputPipeline
|
||||
{
|
||||
private readonly ICommandDispatcher _dispatcher;
|
||||
private readonly ILogger _logger;
|
||||
private IInteractiveCommand? _activeCommand;
|
||||
|
||||
public InputPipeline(ICommandDispatcher dispatcher, ILogger logger)
|
||||
{
|
||||
_dispatcher = dispatcher;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task ProcessInputAsync(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input)) return;
|
||||
|
||||
if (input.StartsWith("/"))
|
||||
{
|
||||
_activeCommand = null;
|
||||
|
||||
var commandInput = input[1..];
|
||||
var result = await _dispatcher.DispatchAsync(commandInput);
|
||||
|
||||
// If the command supports interactivity, save it as active
|
||||
if (result is IInteractiveCommand interactiveCommand && !interactiveCommand.IsCompleted)
|
||||
{
|
||||
_activeCommand = interactiveCommand;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_activeCommand != null)
|
||||
{
|
||||
await _activeCommand.HandleInputAsync(input);
|
||||
|
||||
if (_activeCommand.IsCompleted)
|
||||
_activeCommand = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Info($"Введите /help, чтобы узнать доступные команды!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Govor.ConsoleClient.Services.Interfaces;
|
||||
using Govor.ConsoleClient.Services.Middleware;
|
||||
|
||||
namespace Govor.ConsoleClient.Services.Implementations;
|
||||
|
||||
public delegate Task CommandMiddleware(CommandContext context, Func<Task> next);
|
||||
|
||||
public class MiddlewarePipeline : IMiddlewarePipeline
|
||||
{
|
||||
private readonly IList<ICommandMiddleware> _middlewares;
|
||||
|
||||
public MiddlewarePipeline(IEnumerable<ICommandMiddleware> middlewares)
|
||||
{
|
||||
_middlewares = middlewares.ToList();
|
||||
}
|
||||
|
||||
public Task ExecuteAsync(CommandContext context)
|
||||
{
|
||||
return InvokeNext(0, context);
|
||||
}
|
||||
|
||||
private Task InvokeNext(int index, CommandContext context)
|
||||
{
|
||||
if (index < _middlewares.Count)
|
||||
{
|
||||
return _middlewares[index].InvokeAsync(context, () => InvokeNext(index + 1, context));
|
||||
}
|
||||
return context.Command.ExecuteAsync(context);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user