using System.Management.Automation; using NozCli.Core.Client; namespace Noz.PowerShell.Cmdlets; [Cmdlet(VerbsCommon.Add, "NozLink", SupportsShouldProcess = true)] public sealed class AddNozLinkCmdlet : NozCmdletBase { [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true)] public string Source { get; set; } = ""; [Parameter(Mandatory = true, Position = 1)] public string Target { get; set; } = ""; [Parameter] public SwitchParameter Both { get; set; } [Parameter] public string Section { get; set; } = "Siehe auch"; protected override void ProcessRecord() { if (!ShouldProcess(Source, $"Add-NozLink → {Target}")) return; LinkOne(Source, Target); if (Both) LinkOne(Target, Source); } private void LinkOne(string source, string target) { var note = Run(Api.GetNoteAsync(source)); var updated = NoteLinkHelper.AddLink(note.Markdown, target, Section); if (updated == note.Markdown) { WriteVerbose($"{source} → {target}: bereits verlinkt."); return; } Run(Api.PutNoteAsync(source, updated)); WriteVerbose($"{source} → {target}: verlinkt."); } }