diff --git a/cli/Squidex.CLI/Squidex.CLI/Commands/App_Content.cs b/cli/Squidex.CLI/Squidex.CLI/Commands/App_Content.cs index d3b767b5..bce92da4 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Commands/App_Content.cs +++ b/cli/Squidex.CLI/Squidex.CLI/Commands/App_Content.cs @@ -96,18 +96,20 @@ public async Task Import(ImportArguments arguments) await using (var stream = new FileStream(arguments.File, FileMode.Open, FileAccess.Read)) { - if (arguments.JsonArray) - { - var datas = converter.ReadAsArray(stream); + var datas = converter.ReadAsArray(stream); - await session.ImportAsync(arguments, log, datas); - } - else - { - var datas = converter.ReadAsSeparatedObjects(stream, JsonSeparator); + await session.ImportAsync(arguments, log, datas); + } + } + else if (arguments.Format == Format.JSON_Separated) + { + var converter = new Json2SquidexConverter(arguments.Fields); - await session.ImportAsync(arguments, log, datas); - } + await using (var stream = new FileStream(arguments.File, FileMode.Open, FileAccess.Read)) + { + var datas = converter.ReadAsSeparatedObjects(stream, JsonSeparator); + + await session.ImportAsync(arguments, log, datas); } } else @@ -141,106 +143,99 @@ public async Task Export(ExportArguments arguments) { var session = configuration.StartSession(); - if (arguments.Format == Format.JSON) + string OpenFile(string extension) { - var fileOrFolder = arguments.Output; + var file = arguments.Output; - if (arguments.FilePerContent) + if (string.IsNullOrWhiteSpace(file)) { - if (string.IsNullOrWhiteSpace(fileOrFolder)) - { - fileOrFolder = $"{arguments.Schema}_{DateTime.UtcNow:yyyy-MM-dd-hh-mm-ss}"; - } - - Directory.CreateDirectory(fileOrFolder); + file = $"{arguments.Schema}_{DateTime.UtcNow:yyyy-MM-dd-hh-mm-ss}{extension}"; } - else - { - if (string.IsNullOrWhiteSpace(fileOrFolder)) - { - fileOrFolder = $"{arguments.Schema}_{DateTime.UtcNow:yyyy-MM-dd-hh-mm-ss}.json"; - } - if (File.Exists(fileOrFolder)) - { - File.Delete(fileOrFolder); - } + if (File.Exists(file)) + { + File.Delete(file); } - if (arguments.FilePerContent) - { - await session.ExportAsync(arguments, log, async entity => - { - var fileName = $"{arguments.Schema}_{entity.Id}.json"; - var filePath = Path.Combine(fileOrFolder, fileName); + return file; + } - if (arguments.FullEntities) - { - await Helper.WriteJsonToFileAsync(entity, filePath); - } - else - { - await Helper.WriteJsonToFileAsync(entity.Data, filePath); - } - }); - } - else if (arguments.JsonArray) + if (arguments.Format == Format.JSON && arguments.FilePerContent) + { + var folder = arguments.Output; + + if (string.IsNullOrWhiteSpace(folder)) { - var allRecords = new List(); + folder = $"{arguments.Schema}_{DateTime.UtcNow:yyyy-MM-dd-hh-mm-ss}"; + } - await session.ExportAsync(arguments, log, entity => - { - allRecords.Add(entity); + Directory.CreateDirectory(folder); - return Task.CompletedTask; - }); + await session.ExportAsync(arguments, log, async entity => + { + var fileName = $"{arguments.Schema}_{entity.Id}.json"; + var filePath = Path.Combine(folder, fileName); if (arguments.FullEntities) { - await Helper.WriteJsonToFileAsync(allRecords, fileOrFolder); + await Helper.WriteJsonToFileAsync(entity, filePath); } else { - await Helper.WriteJsonToFileAsync(allRecords.Select(x => x.Data), fileOrFolder); + await Helper.WriteJsonToFileAsync(entity.Data, filePath); } + }); + } + else if (arguments.Format == Format.JSON && !arguments.FilePerContent) + { + var file = OpenFile(".json"); + + var allRecords = new List(); + + await session.ExportAsync(arguments, log, entity => + { + allRecords.Add(entity); + + return Task.CompletedTask; + }); + + if (arguments.FullEntities) + { + await Helper.WriteJsonToFileAsync(allRecords, file); } else { - await using (var stream = new FileStream(fileOrFolder, FileMode.Create, FileAccess.Write)) + await Helper.WriteJsonToFileAsync(allRecords.Select(x => x.Data), file); + } + } + else if (arguments.Format == Format.JSON_Separated && !arguments.FilePerContent) + { + var file = OpenFile(".json"); + + await using (var stream = new FileStream(file, FileMode.Create, FileAccess.Write)) + { + await using (var writer = new StreamWriter(stream)) { - await using (var writer = new StreamWriter(stream)) + await session.ExportAsync(arguments, log, async entity => { - await session.ExportAsync(arguments, log, async entity => + if (arguments.FullEntities) { - if (arguments.FullEntities) - { - await writer.WriteJsonAsync(entity); - } - else - { - await writer.WriteJsonAsync(entity.Data); - } + await writer.WriteJsonAsync(entity); + } + else + { + await writer.WriteJsonAsync(entity.Data); + } - await writer.WriteLineAsync(); - await writer.WriteLineAsync(JsonSeparator); - }); - } + await writer.WriteLineAsync(); + await writer.WriteLineAsync(JsonSeparator); + }); } } } - else + else if (arguments.Format == Format.CSV && !arguments.FilePerContent) { - if (arguments.FilePerContent) - { - throw new CLIException("Multiple files are not supported for CSV export."); - } - - var file = arguments.Output; - - if (string.IsNullOrWhiteSpace(file)) - { - file = $"{arguments.Schema}_{DateTime.UtcNow:yyyy-MM-dd-hh-mm-ss}.csv"; - } + var file = OpenFile(".csv"); var converter = new Squidex2CsvConverter(arguments.Fields); @@ -282,12 +277,17 @@ await session.ExportAsync(arguments, log, async entity => } } } + else + { + throw new CLIException("Multiple files are not supported for this format."); + } } public enum Format { CSV, - JSON + JSON, + JSON_Separated } [Validator(typeof(Validator))] @@ -308,9 +308,6 @@ public sealed class ImportArguments : IImportSettings, IArgumentModel [Option(LongName = "delimiter", Description = "The csv delimiter.")] public string Delimiter { get; set; } = ";"; - [Option(LongName = "array", Description = "Read the JSON as a single json array.")] - public bool JsonArray { get; set; } - [Option(LongName = "format", Description = "Defines the input format.")] public Format Format { get; set; } @@ -360,9 +357,6 @@ public sealed class ExportArguments : IExportSettings, IArgumentModel [Option(LongName = "fields", Description = "Comma separated list of fields. CSV only.")] public string Fields { get; set; } - [Option(LongName = "array", Description = "Write the JSON as a single json array.")] - public bool JsonArray { get; set; } - [Option(LongName = "full", Description = "Write full entities, not only data when exporting as CSV. Default: false.")] public bool FullEntities { get; set; }