Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<Authors>link-foundation</Authors>
<Description>A CLI tool for links manipulation.</Description>
<PackageId>clink</PackageId>
<Version>2.2.2</Version>
<Version>2.3.0</Version>
<PackageLicenseExpression>Unlicense</PackageLicenseExpression>
<RepositoryUrl>https://github.com/link-foundation/link-cli</RepositoryUrl>
</PropertyGroup>
Expand Down
64 changes: 56 additions & 8 deletions Foundation.Data.Doublets.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -70,6 +71,12 @@
afterOption.AddAlias("--links");
afterOption.AddAlias("-a");

var exportOption = new Option<string?>(
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,
Expand All @@ -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<uint>(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<uint>(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)
Expand Down Expand Up @@ -145,10 +178,8 @@
{
PrintAllLinks(decoratedLinks);
}
},
// Explicitly specify the type parameters
dbOption, queryOption, queryArgument, traceOption, structureOption, beforeOption, changesOption, afterOption
);
});


await rootCommand.InvokeAsync(args);

Expand Down Expand Up @@ -189,4 +220,21 @@ static void PrintChange(NamedLinksDecorator<uint> links, DoubletLink linkBefore,
var afterText = linkAfter.IsNull() ? "" : links.Format(linkAfter);
var formattedChange = $"({beforeText}) ({afterText})";
Console.WriteLine(Namify(links, formattedChange));
}

static void ExportToLino(NamedLinksDecorator<uint> links, string filePath)
{
var any = links.Constants.Any;
var query = new DoubletLink(index: any, source: any, target: any);
var linksNotation = new List<string>();

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);
}