feat(porttracker): add library batch-update and batch-map commands

This commit is contained in:
Joseph Doherty
2026-02-27 04:43:11 -05:00
parent 0a6e6bf60d
commit 7769966e2e
3 changed files with 144 additions and 1 deletions

View File

@@ -86,10 +86,118 @@ public static class LibraryCommands
libraryCommand.Add(listCmd);
libraryCommand.Add(mapCmd);
libraryCommand.Add(suggestCmd);
libraryCommand.Add(CreateBatchUpdate(dbOption));
libraryCommand.Add(CreateBatchMap(dbOption));
return libraryCommand;
}
private static Command CreateBatchUpdate(Option<string> dbOption)
{
var cmd = new Command("batch-update", "Bulk update library status");
var idsOpt = BatchFilters.IdsOption();
var statusOpt = BatchFilters.StatusOption();
var executeOpt = BatchFilters.ExecuteOption();
var setStatus = new Option<string>("--set-status") { Description = "New status to set", Required = true };
var setNotes = new Option<string?>("--set-notes") { Description = "Usage notes to set" };
cmd.Add(idsOpt);
cmd.Add(statusOpt);
cmd.Add(executeOpt);
cmd.Add(setStatus);
cmd.Add(setNotes);
cmd.SetAction(parseResult =>
{
var dbPath = parseResult.GetValue(dbOption)!;
var ids = parseResult.GetValue(idsOpt);
var status = parseResult.GetValue(statusOpt);
var execute = parseResult.GetValue(executeOpt);
var newStatus = parseResult.GetValue(setStatus)!;
var notes = parseResult.GetValue(setNotes);
if (string.IsNullOrWhiteSpace(ids) && string.IsNullOrWhiteSpace(status))
{
Console.WriteLine("Error: at least one filter (--ids, --status) is required.");
return;
}
using var db = new Database(dbPath);
var (whereClause, filterParams) = BatchFilters.BuildWhereClause(ids, null, status);
var setClauses = new List<string> { "status = @newStatus" };
var updateParams = new List<(string, object?)> { ("@newStatus", newStatus) };
if (notes is not null)
{
setClauses.Add("dotnet_usage_notes = @newNotes");
updateParams.Add(("@newNotes", notes));
}
BatchFilters.PreviewOrExecute(db, "library_mappings",
"id, go_import_path, status, dotnet_usage_notes",
string.Join(", ", setClauses), updateParams,
whereClause, filterParams, execute);
});
return cmd;
}
private static Command CreateBatchMap(Option<string> dbOption)
{
var cmd = new Command("batch-map", "Bulk map libraries to .NET packages");
var idsOpt = BatchFilters.IdsOption();
var statusOpt = BatchFilters.StatusOption();
var executeOpt = BatchFilters.ExecuteOption();
var setPackage = new Option<string?>("--set-package") { Description = ".NET NuGet package" };
var setNamespace = new Option<string?>("--set-namespace") { Description = ".NET namespace" };
var setNotes = new Option<string?>("--set-notes") { Description = "Usage notes" };
cmd.Add(idsOpt);
cmd.Add(statusOpt);
cmd.Add(executeOpt);
cmd.Add(setPackage);
cmd.Add(setNamespace);
cmd.Add(setNotes);
cmd.SetAction(parseResult =>
{
var dbPath = parseResult.GetValue(dbOption)!;
var ids = parseResult.GetValue(idsOpt);
var status = parseResult.GetValue(statusOpt);
var execute = parseResult.GetValue(executeOpt);
var package = parseResult.GetValue(setPackage);
var ns = parseResult.GetValue(setNamespace);
var notes = parseResult.GetValue(setNotes);
if (string.IsNullOrWhiteSpace(ids) && string.IsNullOrWhiteSpace(status))
{
Console.WriteLine("Error: at least one filter (--ids, --status) is required.");
return;
}
if (package is null && ns is null && notes is null)
{
Console.WriteLine("Error: at least one of --set-package, --set-namespace, --set-notes is required.");
return;
}
using var db = new Database(dbPath);
var (whereClause, filterParams) = BatchFilters.BuildWhereClause(ids, null, status);
var setClauses = new List<string>();
var updateParams = new List<(string, object?)>();
if (package is not null) { setClauses.Add("dotnet_package = @setPackage"); updateParams.Add(("@setPackage", package)); }
if (ns is not null) { setClauses.Add("dotnet_namespace = @setNamespace"); updateParams.Add(("@setNamespace", ns)); }
if (notes is not null) { setClauses.Add("dotnet_usage_notes = @setNotes"); updateParams.Add(("@setNotes", notes)); }
BatchFilters.PreviewOrExecute(db, "library_mappings",
"id, go_import_path, status, dotnet_package, dotnet_namespace",
string.Join(", ", setClauses), updateParams,
whereClause, filterParams, execute);
});
return cmd;
}
private static string Truncate(string? s, int maxLen)
{
if (s is null) return "";