From 29672ece688dc713da1499d5a6cdc64f6843b361 Mon Sep 17 00:00:00 2001 From: benbierens Date: Tue, 24 Oct 2023 15:43:07 +0200 Subject: [PATCH] remove command --- Tools/BiblioTech/Commands/AdminCommand.cs | 49 +++++++++++++++++++-- Tools/BiblioTech/DeploymentsFilesMonitor.cs | 42 ++++++++++++++++++ Tools/BiblioTech/Options/StringOption.cs | 23 ++++++++++ 3 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 Tools/BiblioTech/Options/StringOption.cs diff --git a/Tools/BiblioTech/Commands/AdminCommand.cs b/Tools/BiblioTech/Commands/AdminCommand.cs index 171b755..61d3f5e 100644 --- a/Tools/BiblioTech/Commands/AdminCommand.cs +++ b/Tools/BiblioTech/Commands/AdminCommand.cs @@ -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."); + } } } } diff --git a/Tools/BiblioTech/DeploymentsFilesMonitor.cs b/Tools/BiblioTech/DeploymentsFilesMonitor.cs index d0d135b..c02ed11 100644 --- a/Tools/BiblioTech/DeploymentsFilesMonitor.cs +++ b/Tools/BiblioTech/DeploymentsFilesMonitor.cs @@ -1,4 +1,5 @@ using CodexPlugin; +using Discord; using Newtonsoft.Json; namespace BiblioTech @@ -15,6 +16,47 @@ namespace BiblioTech return deployments; } + public async Task 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(str); + if (deploy != null) + { + var targetFile = Path.Combine(Program.Config.EndpointsPath, Guid.NewGuid().ToString().ToLowerInvariant() + ".json"); + File.WriteAllText(targetFile, str); + deployments = Array.Empty(); + 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(); + return true; + } + } + return false; + } + private void UpdateDeployments() { lastUpdate = DateTime.UtcNow; diff --git a/Tools/BiblioTech/Options/StringOption.cs b/Tools/BiblioTech/Options/StringOption.cs new file mode 100644 index 0000000..3782035 --- /dev/null +++ b/Tools/BiblioTech/Options/StringOption.cs @@ -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 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; + } + } +}