noz-cli/Noz.PowerShell/Cmdlets/RenameNozNoteCmdlet.cs
kryptomrx 5a26de8a11 feat: mv command with atomic note renaming and link rewriting
NoteRenamer fetches inbound graph edges, creates the note under the new
slug (patching the topic frontmatter field if the prefix changes),
rewrites all wikilinks and markdown links in linking notes, then deletes
the old slug. Tests cover wikilink/markdown-link replacement and topic
patching.
2026-05-17 00:27:53 +02:00

50 lines
1.5 KiB
C#

using System.Management.Automation;
using NozCli.Core.Client;
namespace Noz.PowerShell.Cmdlets;
[Cmdlet(VerbsCommon.Rename, "NozNote", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[OutputType(typeof(NozNote))]
public sealed class RenameNozNoteCmdlet : NozCmdletBase
{
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true)]
public string Slug { get; set; } = "";
[Parameter(Mandatory = true, Position = 1)]
public string NewSlug { get; set; } = "";
[Parameter]
public SwitchParameter PassThru { get; set; }
protected override void ProcessRecord()
{
if (Slug == NewSlug)
{
WriteWarning("Quelle und Ziel sind identisch — nichts zu tun.");
return;
}
if (!ShouldProcess(Slug, $"Rename-NozNote → {NewSlug}")) return;
var steps = new List<string>();
var result = Run(NoteRenamer.ExecuteAsync(
Api, Slug, NewSlug,
onProgress: msg => WriteVerbose(msg)));
WriteVerbose($"Fertig: {result.LinksUpdated} Link(s) aktualisiert.");
if (result.LinksUpdated > 0)
{
foreach (var updated in result.UpdatedNotes)
WriteVerbose($" • {updated}");
}
if (PassThru)
{
var notes = Run(Api.GetNotesAsync());
var renamed = notes.Find(n => n.Slug == NewSlug);
if (renamed is not null) WriteObject(NozNote.From(renamed));
}
}
}