#!/usr/bin/env pwsh [CmdletBinding()] param( [Parameter(HelpMessage='Path to JSON file; default: read from stdin')] [string]$JsonFile = '-', [Parameter(HelpMessage='API base URL')] [string]$ApiUrl, [Parameter(HelpMessage='API key for authentication')] [string]$ApiKey, [Parameter(HelpMessage='Show help')] [switch]$Help ) function Show-Usage { $usage = @" Usage: Upload-JMESPath.ps1 [-ApiUrl ] [-ApiKey ] [-JsonFile ] Parameters: -ApiUrl The base URL of the JMESPath Playground API (default: http://localhost:3000 or $env:JMESPATH_PLAYGROUND_API_URL) -ApiKey The API key for authentication (can also be set via JMESPATH_PLAYGROUND_API_KEY) -JsonFile The JSON file to upload (default: stdin if not specified) -Help Show this help message and exit "@ Write-Output $usage } if ($Help) { Show-Usage; exit 0 } # Apply environment defaults when parameters are not provided if (-not $ApiUrl) { if ($env:JMESPATH_PLAYGROUND_API_URL) { $ApiUrl = $env:JMESPATH_PLAYGROUND_API_URL } else { $ApiUrl = 'http://localhost:3000' } } if (-not $ApiKey) { $ApiKey = $env:JMESPATH_PLAYGROUND_API_KEY } # Read JSON body from file or stdin try { if ($JsonFile -eq '-' -or [string]::IsNullOrEmpty($JsonFile)) { $Body = [Console]::In.ReadToEnd() } else { if (-not (Test-Path -Path $JsonFile)) { Write-Error "JSON file not found: $JsonFile" exit 2 } $Body = Get-Content -Raw -Path $JsonFile } } catch { Write-Error "Failed to read JSON input: $($_.Exception.Message)" exit 2 } # Prepare headers $Headers = @{ 'Accept' = 'application/json' } if (-not [string]::IsNullOrEmpty($ApiKey)) { $Headers['X-API-Key'] = $ApiKey } # POST to API $Uri = "$ApiUrl/api/v1/upload" try { $response = Invoke-RestMethod -Uri $Uri -Method Post -Headers $Headers -ContentType 'application/json' -Body $Body -ErrorAction Stop if ($null -ne $response) { $response | ConvertTo-Json -Depth 10 } else { Write-Output "Upload completed." } } catch { Write-Error "Upload failed: $($_.Exception.Message)" exit 3 }