noz-cli/Noz.PowerShell/Cmdlets/SetNozNoteStatusCmdlet.cs
kryptomrx f45b31defa feat: graph and search API client with PS analysis cmdlets
Get-NozGraph, Find-NozNote, Measure-NozGarden, Set-NozNoteStatus,
Get-NozOrphans — graph edges, Meilisearch search, garden stats,
frontmatter status patch, orphan detection from inbound link analysis.
2026-05-17 00:27:45 +02:00

49 lines
1.6 KiB
C#

using System.Management.Automation;
using System.Text.RegularExpressions;
namespace Noz.PowerShell.Cmdlets;
[Cmdlet(VerbsCommon.Set, "NozNoteStatus", SupportsShouldProcess = true)]
[OutputType(typeof(NozNote))]
public sealed partial class SetNozNoteStatusCmdlet : NozCmdletBase
{
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true)]
public string Slug { get; set; } = "";
[Parameter(Mandatory = true, Position = 1)]
[ValidateSet("seed", "budding", "evergreen")]
public string Status { get; set; } = "";
[Parameter]
public SwitchParameter PassThru { get; set; }
[GeneratedRegex(@"(?m)^(status:\s*)(?:seed|budding|evergreen)(\s*)$")]
private static partial Regex StatusPattern();
protected override void ProcessRecord()
{
var content = Run(Api.GetNoteAsync(Slug));
var patched = StatusPattern().Replace(content.Markdown, $"$1{Status}$2");
if (patched == content.Markdown)
{
WriteVerbose($"{Slug}: status is already '{Status}', no change.");
if (PassThru) WriteObject(Run(Api.GetNotesAsync()).Find(n => n.Slug == Slug) is { } n
? NozNote.From(n) : null);
return;
}
if (!ShouldProcess(Slug, $"Set status → {Status}")) return;
Run(Api.PutNoteAsync(Slug, patched));
WriteVerbose($"{Slug}: status updated to '{Status}'.");
if (PassThru)
{
var notes = Run(Api.GetNotesAsync());
var updated = notes.Find(n => n.Slug == Slug);
if (updated is not null) WriteObject(NozNote.From(updated));
}
}
}