remove command

This commit is contained in:
benbierens 2023-10-24 15:43:07 +02:00
parent a0461a446e
commit 29672ece68
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
3 changed files with 111 additions and 3 deletions

View File

@ -9,6 +9,7 @@ namespace BiblioTech.Commands
private readonly ReportCommand reportCommand;
private readonly DeployListCommand deployListCommand;
private readonly DeployUploadCommand deployUploadCommand;
private readonly DeployRemoveCommand deployRemoveCommand;
public override string Name => "admin";
public override string StartingMessage => "...";
@ -20,6 +21,7 @@ namespace BiblioTech.Commands
reportCommand = new ReportCommand();
deployListCommand = new DeployListCommand(monitor);
deployUploadCommand = new DeployUploadCommand(monitor);
deployRemoveCommand = new DeployRemoveCommand(monitor);
}
public override CommandOption[] Options => new CommandOption[]
@ -28,6 +30,7 @@ namespace BiblioTech.Commands
reportCommand,
deployListCommand,
deployUploadCommand,
deployRemoveCommand
};
protected override async Task Invoke(CommandContext context)
@ -42,6 +45,7 @@ namespace BiblioTech.Commands
await reportCommand.CommandHandler(context);
await deployListCommand.CommandHandler(context);
await deployUploadCommand.CommandHandler(context);
await deployRemoveCommand.CommandHandler(context);
}
public class ClearUserAssociationCommand : SubCommandOption
@ -122,7 +126,7 @@ namespace BiblioTech.Commands
private string FormatDeployment(CodexDeployment deployment)
{
var m = deployment.Metadata;
return $"{m.Name} ({m.StartUtc.ToString("o")})";
return $"'{m.Name}' ({m.StartUtc.ToString("o")})";
}
}
@ -147,9 +151,48 @@ namespace BiblioTech.Commands
var file = await fileOption.Parse(context);
if (file == null) return;
await context.Command.FollowupAsync("Received: " + file.Size);
var result = await monitor.DownloadDeployment(file);
if (result)
{
await context.Command.FollowupAsync("Success!");
}
else
{
await context.Command.FollowupAsync("That didn't work.");
}
}
}
// todo pass to monitor, add to folder.
public class DeployRemoveCommand : SubCommandOption
{
private readonly DeploymentsFilesMonitor monitor;
private readonly StringOption stringOption = new StringOption(
name: "deployment name",
description: "Name of deployment to remove.",
isRequired: true);
public DeployRemoveCommand(DeploymentsFilesMonitor monitor)
: base("remove", "Removes a deployment file.")
{
this.monitor = monitor;
}
public override CommandOption[] Options => new[] { stringOption };
protected override async Task onSubCommand(CommandContext context)
{
var str = await stringOption.Parse(context);
if (string.IsNullOrEmpty(str)) return;
var result = monitor.DeleteDeployment(str);
if (result)
{
await context.Command.FollowupAsync("Success!");
}
else
{
await context.Command.FollowupAsync("That didn't work.");
}
}
}
}

View File

@ -1,4 +1,5 @@
using CodexPlugin;
using Discord;
using Newtonsoft.Json;
namespace BiblioTech
@ -15,6 +16,47 @@ namespace BiblioTech
return deployments;
}
public async Task<bool> DownloadDeployment(IAttachment file)
{
using var http = new HttpClient();
var response = await http.GetAsync(file.Url);
var str = await response.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(str)) return false;
try
{
var deploy = JsonConvert.DeserializeObject<CodexDeployment>(str);
if (deploy != null)
{
var targetFile = Path.Combine(Program.Config.EndpointsPath, Guid.NewGuid().ToString().ToLowerInvariant() + ".json");
File.WriteAllText(targetFile, str);
deployments = Array.Empty<CodexDeployment>();
return true;
}
}
catch { }
return false;
}
public bool DeleteDeployment(string deploymentName)
{
var path = Program.Config.EndpointsPath;
if (!Directory.Exists(path)) return false;
var files = Directory.GetFiles(path);
foreach ( var file in files)
{
var deploy = ProcessFile(file);
if (deploy != null && deploy.Metadata.Name == deploymentName)
{
File.Delete(file);
deployments = Array.Empty<CodexDeployment>();
return true;
}
}
return false;
}
private void UpdateDeployments()
{
lastUpdate = DateTime.UtcNow;

View File

@ -0,0 +1,23 @@
using Discord;
namespace BiblioTech.Options
{
public class StringOption : CommandOption
{
public StringOption(string name, string description, bool isRequired)
: base(name, description, type: ApplicationCommandOptionType.String, isRequired)
{
}
public async Task<string?> Parse(CommandContext context)
{
var strData = context.Options.SingleOrDefault(o => o.Name == Name);
if (strData == null)
{
await context.Command.FollowupAsync("String option not received.");
return null;
}
return strData.Value as string;
}
}
}