From b861a69799834f263a830e6c5359c1b7dba4c648 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Sun, 4 Sep 2022 18:14:22 +0300 Subject: [PATCH] Recognize existing directories as valid output paths in CLI Closes #917 --- .../Commands/Base/ExportCommandBase.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs b/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs index e14728d..c1621d8 100644 --- a/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs +++ b/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs @@ -26,7 +26,7 @@ public abstract class ExportCommandBase : TokenCommandBase [CommandOption( "output", 'o', - Description = "Output file or directory path." + Description = "Output file or directory path. Directory path should end in a slash." )] public string OutputPath { @@ -97,8 +97,7 @@ public abstract class ExportCommandBase : TokenCommandBase protected async ValueTask ExecuteAsync(IConsole console, IReadOnlyList channels) { - var cancellationToken = console.RegisterCancellationHandler(); - + // Reuse media option should only be used when the media option is set. // https://github.com/Tyrrrz/DiscordChatExporter/issues/425 if (ShouldReuseMedia && !ShouldDownloadMedia) { @@ -107,14 +106,26 @@ public abstract class ExportCommandBase : TokenCommandBase ); } + // Make sure the user does not try to export all channels into a single file. + // Output path must either be a directory, or contain template tokens. // https://github.com/Tyrrrz/DiscordChatExporter/issues/799 - if (channels.Count > 1 && !PathEx.IsDirectoryPath(OutputPath) && !OutputPath.Contains('%')) + // https://github.com/Tyrrrz/DiscordChatExporter/issues/917 + var isValidOutputPath = + // Anything is valid when exporting a single channel + channels.Count <= 1 || + // When using template tokens, assume the user knows what they're doing + OutputPath.Contains('%') || + // Otherwise, require an existing directory or an unambiguous directory path + Directory.Exists(OutputPath) || PathEx.IsDirectoryPath(OutputPath); + + if (!isValidOutputPath) { throw new CommandException( "Attempted to export multiple channels, but the output path is neither a directory nor a template." ); } + var cancellationToken = console.RegisterCancellationHandler(); var errors = new ConcurrentDictionary(); // Export