Add AddFingerprintToApplicationCommand

This commit is contained in:
2021-06-07 18:38:23 +02:00
parent 4d51d4f00e
commit 3658bdcba0

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Akari.Prototype.Server.Options;
using Akari.Prototype.Server.Services;
using Akari.Prototype.Server.Utils;
using Akari.Prototype.Server.Utils.Extensions;
@@ -9,6 +11,10 @@ using CliFx;
using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Spectre.Console;
namespace Akari.Prototype.Server.Cli.Commands
@@ -81,12 +87,77 @@ namespace Akari.Prototype.Server.Cli.Commands
throw new CommandException($"The application {Name} doesn't exist");
}
console.AsAnsiConsole().Markup($"[green]Successfully deleted {Name}[/]");
console.AsAnsiConsole().MarkupLine($"[green]Successfully deleted {Name}[/]");
return default;
}
}
[Command("app f", Description = "Add a fingerprint to and application")]
public class AddFingerprintToApplicationCommand : ICommand
{
[CommandParameter(0, Description = "Application to add fingerprint to")]
public string Application { get; init; }
[CommandParameter(1, Description = "Fingerprint to add")]
public string Fingerprint { get; init; }
private readonly IServiceProvider _serviceProvider;
private readonly IApplicationsManager _applications;
private readonly IMasterKeyService _masterKeyService;
public AddFingerprintToApplicationCommand(IServiceProvider serviceProvider, IApplicationsManager applications, IMasterKeyService masterKeyService)
{
_serviceProvider = serviceProvider;
_applications = applications;
_masterKeyService = masterKeyService;
}
public async ValueTask ExecuteAsync(IConsole console)
{
var ansiConsole = console.AsAnsiConsole();
if (!_applications.Contains(Application))
{
throw new CommandException("No application exist with this name");
}
if (_applications.IsFingerprintRegistered(Application, Fingerprint))
{
throw new CommandException($"'{Fingerprint}' is already registered for '{Application}'");
}
if (!CliUtils.Login(_masterKeyService, ansiConsole))
{
throw new CommandException("Can't proceed without master service login");
}
using var scope = _serviceProvider.CreateScope();
var services = scope.ServiceProvider;
using var provider = new TcpProviderService(services.GetRequiredService<ILogger<TcpProviderService>>(),
services.GetRequiredService<IOptions<TcpProviderOptions>>(),
services.GetRequiredService<IHostApplicationLifetime>(),
services.GetRequiredService<IFingerprintManager>());
var source = new CancellationTokenSource();
await provider.StartAsync(source.Token);
ansiConsole.WriteLine("Press a key when the fingerprint has been auth");
console.Input.Read();
if (!_applications.AddFingerprint(Application, Fingerprint))
{
throw new CommandException($"Fingerprint '{Fingerprint}' has not been auth");
}
ansiConsole.WriteLine($"[green]Successfully added '{Fingerprint}' to '{Application}'[/]");
}
}
private readonly IApplicationsManager _applications;
public ApplicationCommands(IApplicationsManager applications)