diff --git a/Foundation.Data.Doublets.Cli/Foundation.Data.Doublets.Cli.csproj b/Foundation.Data.Doublets.Cli/Foundation.Data.Doublets.Cli.csproj index 1d8f9ab..e4d6c4d 100644 --- a/Foundation.Data.Doublets.Cli/Foundation.Data.Doublets.Cli.csproj +++ b/Foundation.Data.Doublets.Cli/Foundation.Data.Doublets.Cli.csproj @@ -15,7 +15,7 @@ link-foundation A CLI tool for links manipulation. clink - 2.2.2 + 2.3.0 Unlicense https://github.com/link-foundation/link-cli diff --git a/Foundation.Data.Doublets.Cli/Program.cs b/Foundation.Data.Doublets.Cli/Program.cs index 1f9bfed..2f8f640 100644 --- a/Foundation.Data.Doublets.Cli/Program.cs +++ b/Foundation.Data.Doublets.Cli/Program.cs @@ -1,4 +1,5 @@ using System.CommandLine; +using System.CommandLine.Invocation; using Platform.Data; using Platform.Data.Doublets; using Platform.Data.Doublets.Memory.United.Generic; @@ -70,6 +71,12 @@ afterOption.AddAlias("--links"); afterOption.AddAlias("-a"); +var exportOption = new Option( + name: "--export", + description: "Export the database to a LiNo file" +); +exportOption.AddAlias("-e"); + var rootCommand = new RootCommand("LiNo CLI Tool for managing links data store") { dbOption, @@ -79,13 +86,39 @@ structureOption, beforeOption, changesOption, - afterOption + afterOption, + exportOption }; -rootCommand.SetHandler( - (string db, string queryOptionValue, string queryArgumentValue, bool trace, uint? structure, bool before, bool changes, bool after) => +rootCommand.SetHandler((InvocationContext context) => { - var decoratedLinks = new NamedLinksDecorator(db, trace); + var db = context.ParseResult.GetValueForOption(dbOption); + var queryOptionValue = context.ParseResult.GetValueForOption(queryOption); + var queryArgumentValue = context.ParseResult.GetValueForArgument(queryArgument); + var trace = context.ParseResult.GetValueForOption(traceOption); + var structure = context.ParseResult.GetValueForOption(structureOption); + var before = context.ParseResult.GetValueForOption(beforeOption); + var changes = context.ParseResult.GetValueForOption(changesOption); + var after = context.ParseResult.GetValueForOption(afterOption); + var export = context.ParseResult.GetValueForOption(exportOption); + + var decoratedLinks = new NamedLinksDecorator(db!, trace); + + // If --export is provided, handle it separately + if (!string.IsNullOrWhiteSpace(export)) + { + try + { + ExportToLino(decoratedLinks, export); + Console.WriteLine($"Database exported to {export}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Error exporting database to {export}: {ex.Message}"); + Environment.Exit(1); + } + return; // Exit after handling --export + } // If --structure is provided, handle it separately if (structure.HasValue) @@ -145,10 +178,8 @@ { PrintAllLinks(decoratedLinks); } - }, - // Explicitly specify the type parameters - dbOption, queryOption, queryArgument, traceOption, structureOption, beforeOption, changesOption, afterOption -); + }); + await rootCommand.InvokeAsync(args); @@ -189,4 +220,21 @@ static void PrintChange(NamedLinksDecorator links, DoubletLink linkBefore, var afterText = linkAfter.IsNull() ? "" : links.Format(linkAfter); var formattedChange = $"({beforeText}) ({afterText})"; Console.WriteLine(Namify(links, formattedChange)); +} + +static void ExportToLino(NamedLinksDecorator links, string filePath) +{ + var any = links.Constants.Any; + var query = new DoubletLink(index: any, source: any, target: any); + var linksNotation = new List(); + + links.Each(query, link => + { + var formattedLink = links.Format(link); + var namedLink = Namify(links, formattedLink); + linksNotation.Add(namedLink); + return links.Constants.Continue; + }); + + File.WriteAllLines(filePath, linksNotation); } \ No newline at end of file