Private/Remove-GhostSyncResource.ps1
|
function Remove-GhostSyncResource { <# .SYNOPSIS Removes the posts, pages or tags whose file is gone. .DESCRIPTION The Sync commands call this when they are given a PreviousPath: the files as they were at the previous run, such as the previous commit of a repository. A file there describes a post, a page or a tag by its slug. When no current file has that slug any more, the resource with that slug is removed from the site. Slugs are compared, not file names. A file that moved to another folder keeps its slug, so nothing is removed for it. A file whose slug changed describes another resource now, so the one with the old slug is removed. Nothing is removed in these cases: - The previous file cannot be read. That is a warning, because a file with a mistake in it may be the reason that it was removed. - The site has no resource with the slug. - The site has more than one resource with the slug. That is a warning. Removing cannot be undone, so this function asks for confirmation the way Remove-GhostPost does. A Sync command that is run with -Confirm:$false or with -WhatIf passes that on by itself. The result is a row of the type the Sync command returns, with the action Removed. In a dry run the action is WouldRemove and nothing is removed. .EXAMPLE Remove-GhostSyncResource -Kind Post -PreviousPath ./before/posts -CurrentSlug 'my-post' -Fields $fields Removes every post that a file in before/posts describes, except the post with the slug my-post. .EXAMPLE Remove-GhostSyncResource -Kind Tag -PreviousPath ./before/tags -CurrentSlug @() -Fields $fields -WhatIf Returns a row with the action WouldRemove for every tag that a file in before/tags describes. #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] [OutputType([pscustomobject])] param( # What the files describe. [Parameter(Mandatory)] [ValidateSet('Post', 'Page', 'Tag')] [string] $Kind, # Files or folders with the files as they were at the previous run. [Parameter(Mandatory)] [string[]] $PreviousPath, # The slugs of the current files. A resource with one of these slugs is kept. [Parameter(Mandatory)] [AllowEmptyCollection()] [string[]] $CurrentSlug, # The fields the front matter may set: parameter name = name of the field in the Ghost API. [Parameter(Mandatory)] [System.Collections.IDictionary] $Fields, # Also read the files in the folders below a previous folder. [switch] $Recurse ) $noun = $Kind.ToLowerInvariant() # The slugs that are settled: a current file has them, or an earlier previous file was handled already. $settled = @{} foreach ($slug in $CurrentSlug) { $settled[$slug] = $true } foreach ($file in Get-GhostSyncFile -Path $PreviousPath -Recurse:$Recurse) { try { $source = Read-GhostPostFile -Path $file.FullName -Fields $Fields } catch { $reason = $_.Exception.Message Write-Warning -Message "Nothing is removed for a previous file that cannot be read. $reason" continue } $slug = [string] $source.Parameters['Slug'] if ($settled.ContainsKey($slug)) { continue } $settled[$slug] = $true # Quoted, so that a slug of digits only is compared as text. $filter = "slug:'$slug'" $found = switch ($Kind) { 'Post' { @(Get-GhostPost -Filter $filter) } 'Page' { @(Get-GhostPage -Filter $filter) } 'Tag' { @(Get-GhostTag -Filter $filter) } } $found = @($found) if ($found.Count -eq 0) { continue } if ($found.Count -gt 1) { Write-Warning -Message "The site has $($found.Count) ${noun}s with the slug '$slug'. None is removed." continue } $resource = $found[0] # A tag has a name where a post and a page have a title. $label = if ($Kind -eq 'Tag') { $resource.name } else { $resource.title } $action = 'Removed' if ($PSCmdlet.ShouldProcess([string] $label, "Remove the $noun '$slug'")) { switch ($Kind) { 'Post' { Remove-GhostPost -Id $resource.id -Confirm:$false } 'Page' { Remove-GhostPage -Id $resource.id -Confirm:$false } 'Tag' { Remove-GhostTag -Id $resource.id -Confirm:$false } } } elseif ($WhatIfPreference) { $action = 'WouldRemove' } else { # The answer to the question was no. Nothing is removed and nothing is reported. continue } if ($Kind -eq 'Tag') { [pscustomobject]@{ PSTypeName = 'Ghost.TagSync' Action = $action Name = $resource.name Slug = $resource.slug Id = $resource.id Path = $file.FullName } continue } # Outside the hash literal: Windows PowerShell cannot parse an if statement as a value in one. $url = $resource.PSObject.Properties['url'] $address = if ($url) { $url.Value } else { $null } $status = $resource.PSObject.Properties['status'] $state = if ($status) { $status.Value } else { $null } [pscustomobject]@{ PSTypeName = "Ghost.${Kind}Sync" Action = $action Status = $state Slug = $resource.slug Title = $resource.title Id = $resource.id Url = $address Path = $file.FullName UploadedImages = @() } } } |