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.
49 lines
1.6 KiB
C#
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));
|
|
}
|
|
}
|
|
}
|