|
| 1 | +function Remove-Neocities |
| 2 | +{ |
| 3 | + <# |
| 4 | + .SYNOPSIS |
| 5 | + Removes files from neocities |
| 6 | + .DESCRIPTION |
| 7 | + Removes files from a neocities site using the neocities API. |
| 8 | + #> |
| 9 | + [CmdletBinding(DefaultParameterSetName='delete',SupportsShouldProcess,ConfirmImpact='High')] |
| 10 | + param( |
| 11 | + # The name of the file to remove. |
| 12 | + [Parameter(Mandatory,ValueFromPipelineByPropertyName)] |
| 13 | + [Alias('FullName','Path')] |
| 14 | + [string[]] |
| 15 | + $FileName, |
| 16 | + |
| 17 | + |
| 18 | + # The neocities credential |
| 19 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 20 | + [Alias( |
| 21 | + 'Credentials', # Plural aliases are nice |
| 22 | + 'PSCredential', # so are parameters that match the type name. |
| 23 | + 'NeocitiesCredential', # A contextual alias is a good idea, too. |
| 24 | + 'NeocitiesCredentials' # And you may need to pluralize that contextual alias. |
| 25 | + )] |
| 26 | + [PSCredential] |
| 27 | + $Credential, |
| 28 | + |
| 29 | + # The neocities access token. |
| 30 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 31 | + [string] |
| 32 | + $AccessToken |
| 33 | + ) |
| 34 | + |
| 35 | + begin { |
| 36 | + $NeocitiesApi = "https://neocities.org/api" |
| 37 | + } |
| 38 | + |
| 39 | + process { |
| 40 | + $parameterSet = $PSCmdlet.ParameterSetName |
| 41 | + $psuedoNamespace = "neocities" |
| 42 | + $pseudoType = "$parameterSet" |
| 43 | + $InvokeSplat = [Ordered]@{ |
| 44 | + Uri = "$NeocitiesApi", $PSCmdlet.ParameterSetName -join '/' |
| 45 | + Method = 'POST' |
| 46 | + } |
| 47 | + |
| 48 | + # If an access token was provided |
| 49 | + if ($AccessToken) |
| 50 | + { |
| 51 | + # use it |
| 52 | + $InvokeSplat.Headers = @{Authorization = "Bearer $AccessToken"} |
| 53 | + # and cache it for later use |
| 54 | + $script:NeocitiesAccessToken = $AccessToken |
| 55 | + } |
| 56 | + elseif ($Credential) |
| 57 | + { |
| 58 | + # If a credential was provided, use it |
| 59 | + $InvokeSplat.Credential = $Credential |
| 60 | + # and cache it for later use |
| 61 | + $script:NeoCitiesCredential = $Credential |
| 62 | + # (don't forget to set authentication to basic) |
| 63 | + $InvokeSplat.Authentication = 'Basic' |
| 64 | + } |
| 65 | + elseif ($script:NeocitiesAccessToken) { |
| 66 | + # If we had a cached access token, use it |
| 67 | + $InvokeSplat.Headers = @{Authorization = "Bearer $($script:NeocitiesAccessToken)"} |
| 68 | + } |
| 69 | + elseif ($script:NeoCitiesCredential) { |
| 70 | + # If we had a cached credential, use it |
| 71 | + $InvokeSplat.Credential = $script:NeoCitiesCredential |
| 72 | + # and don't forget to set authentication to basic. |
| 73 | + $InvokeSplat.Authentication = 'Basic' |
| 74 | + } |
| 75 | + |
| 76 | + # If neither an access token nor a credential was provided, we can't do anything. |
| 77 | + if (-not $InvokeSplat.Credential -and -not $InvokeSplat.Headers) |
| 78 | + { |
| 79 | + # so error out. |
| 80 | + Write-Error "No -Credential provided" |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + # For every file name provided, we need to remove it from the neocities site. |
| 85 | + foreach ($file in $fileName) { |
| 86 | + # Despite the name taking an array, we need to remove one file at a time. |
| 87 | + $InvokeSplat.Body = |
| 88 | + [web.httputility]::UrlEncode("filenames[]"),'=',[web.httputility]::UrlEncode($file) -join '' |
| 89 | + Write-Verbose "Requesting $($InvokeSplat.Uri)" |
| 90 | + # If -WhatIf was specified, we need to remove the credential and headers from the splat |
| 91 | + if ($WhatIfPreference) { |
| 92 | + $splatCopy = [Ordered]@{} + $InvokeSplat |
| 93 | + $splatCopy.Remove('Credential') |
| 94 | + $splatCopy.Remove('Headers') |
| 95 | + # and then output the splat to the pipeline |
| 96 | + $splatCopy |
| 97 | + continue |
| 98 | + } |
| 99 | + # If we did not confirm the deletion |
| 100 | + if (-not $PSCmdlet.ShouldProcess("Delete $file")) { |
| 101 | + # skip it. |
| 102 | + continue |
| 103 | + } |
| 104 | + |
| 105 | + # Get the response from neocities. |
| 106 | + $neocitiesResponse = Invoke-RestMethod @InvokeSplat |
| 107 | + # and decorate any response so that we know it was a deletion. |
| 108 | + foreach ($neoResponse in $neocitiesResponse) { |
| 109 | + $neoResponse.pstypenames.clear() |
| 110 | + $neoResponse.pstypenames.insert(0, ($psuedoNamespace, $pseudoType -join '.')) |
| 111 | + $neoResponse |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments