Skip to content

Instantly share code, notes, and snippets.

@tormodfj
Created July 11, 2019 07:16
Show Gist options
  • Save tormodfj/bca31ceb336a33c2e91948e7ebf8bdd2 to your computer and use it in GitHub Desktop.
Save tormodfj/bca31ceb336a33c2e91948e7ebf8bdd2 to your computer and use it in GitHub Desktop.
Parsing Swagger and listing x-required-rights
param (
[string]$SwaggerUrl,
[switch]$Pretty = $false
)
$paths = Invoke-WebRequest $swaggerUrl | ConvertFrom-Json | Select-Object -ExpandProperty paths
$result = @() # result array
foreach ($prop in $paths.PSObject.Properties) {
$pathName = $prop.Name
$pathVerbs = $prop.Value
foreach ($prop in $pathVerbs.PSObject.Properties) {
$verb = $prop.Name
$rights = $prop.Value.'x-required-rights'
if ($null -ne $rights) {
$result += [PSCustomObject] @{ Verb = $verb.ToUpper() ; Path = $pathName; Rights = $rights }
}
}
}
if ($Pretty -ne $true) {
$result
} else {
function VerbColor($verb) {
switch ($verb) {
"GET" { "Blue" }
"POST" { "Green" }
"DELETE" { "Red" }
"PUT" { "Yellow" }
"PATCH" { "Cyan" }
default { "White" }
}
}
foreach ($op in $result) {
$verb = $op.Verb
$path = $op.Path
$rights = $op.Rights
Write-Host $(" {0,-6} " -f $verb) -BackgroundColor $(VerbColor($verb)) -ForegroundColor Black -NoNewline
Write-Host $(" {0}" -f $path)
Write-Host $(" {0}" -f $rights) -ForegroundColor White
Write-Host # new-line
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment