From 614bd8590ddaae0dd23547de7f58b6dfebf22395 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Mon, 13 Aug 2018 23:25:06 +0300 Subject: [PATCH] Generate file name if given output path CLI argument is a directory Closes #67 --- DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs | 4 ++-- DiscordChatExporter.Core/Services/ExportService.cs | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs b/DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs index a791f2d..8188517 100644 --- a/DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs +++ b/DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs @@ -38,9 +38,9 @@ namespace DiscordChatExporter.Cli.Verbs // Generate file path if not set var filePath = Options.FilePath; - if (filePath.IsBlank()) + if (filePath == null || filePath.EndsWith("/") || filePath.EndsWith("\\")) { - filePath = $"{guild.Name} - {channel.Name}.{Options.ExportFormat.GetFileExtension()}" + filePath += $"{guild.Name} - {channel.Name}.{Options.ExportFormat.GetFileExtension()}" .Replace(Path.GetInvalidFileNameChars(), '_'); } diff --git a/DiscordChatExporter.Core/Services/ExportService.cs b/DiscordChatExporter.Core/Services/ExportService.cs index 15a17cd..0d623aa 100644 --- a/DiscordChatExporter.Core/Services/ExportService.cs +++ b/DiscordChatExporter.Core/Services/ExportService.cs @@ -2,6 +2,7 @@ using DiscordChatExporter.Core.Models; using Scriban; using Scriban.Runtime; +using Tyrrrz.Extensions; namespace DiscordChatExporter.Core.Services { @@ -37,6 +38,11 @@ namespace DiscordChatExporter.Core.Services var templateModel = new TemplateModel(format, log, _settingsService.DateFormat); context.PushGlobal(templateModel.GetScriptObject()); + // Create directory + var dirPath = Path.GetDirectoryName(filePath); + if (dirPath.IsNotBlank()) + Directory.CreateDirectory(dirPath); + // Render output using (var output = File.CreateText(filePath)) {