From 929d2ee5d2dfdf11b01a6b39443acc0c38328011 Mon Sep 17 00:00:00 2001 From: Slawomir Koszewski Date: Fri, 30 Jan 2026 00:04:02 +0100 Subject: [PATCH] Add upload scripts for PowerShell and NodeJS. --- bin/Upload-JMESPath.ps1 | 72 +++++++++++++++++++++++++++++++ bin/upload-jmespath.mjs | 94 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100755 bin/Upload-JMESPath.ps1 create mode 100755 bin/upload-jmespath.mjs diff --git a/bin/Upload-JMESPath.ps1 b/bin/Upload-JMESPath.ps1 new file mode 100755 index 0000000..08aa4c0 --- /dev/null +++ b/bin/Upload-JMESPath.ps1 @@ -0,0 +1,72 @@ +#!/usr/bin/env pwsh +[CmdletBinding()] +param( + [Parameter(Position=0, HelpMessage='API base URL')] + [string]$ApiUrl, + + [Parameter(HelpMessage='API key for authentication')] + [string]$ApiKey, + + [Parameter(HelpMessage='Path to JSON file; default: read from stdin')] + [string]$JsonFile = '-', + + [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 +} diff --git a/bin/upload-jmespath.mjs b/bin/upload-jmespath.mjs new file mode 100755 index 0000000..6f18948 --- /dev/null +++ b/bin/upload-jmespath.mjs @@ -0,0 +1,94 @@ +#!/usr/bin/env node + +import fs from "fs"; +import path from "path"; + +// Parse command-line arguments +import { parseArgs } from "util"; + +const args = parseArgs({ + options: { + "api-url": { + type: "string", + short: "a", + default: process.env.JMESPATH_PLAYGROUND_API_URL || "http://localhost:3000", + }, + "api-key": { + type: "string", + short: "k", + default: process.env.JMESPATH_PLAYGROUND_API_KEY || "", + }, + "json-file": { + type: "string", + short: "j", + default: "", + }, + help: { type: "boolean", short: "h" }, + }, +}); + +// Show help message +if (args.values.help) { + console.log(` +Usage: upload-jmespath.mjs [options] + +Options: + -a, --api-url API base URL (default: http://localhost:3000) + -k, --api-key API key for authentication + -j, --json-file Path to the JSON file to upload + -h, --help Show this help message +`); + process.exit(0); +} + +// Read the JSON from the specfied file or from stdin if no file is provided +async function readJson(filePath) { + if (filePath) { + const absolutePath = path.resolve(filePath); + const fileContent = fs.readFileSync(absolutePath, "utf-8"); + return JSON.parse(fileContent); + } else { + return new Promise((resolve, reject) => { + let data = ""; + process.stdin.on("data", (chunk) => { + data += chunk; + }); + process.stdin.on("end", () => { + try { + resolve(JSON.parse(data)); + } catch (error) { + reject(error); + } + }); + }); + } +} + +// Upload the JSON data to the API using built-in fetch +async function uploadJson(apiUrl, apiKey, jsonData) { + const response = await fetch(`${apiUrl}/api/v1/upload`, { + method: "POST", + headers: { + "Content-Type": "application/json", + ...(apiKey ? { "X-API-Key": `${apiKey}` } : {}), + }, + body: JSON.stringify(jsonData), + }); + if (!response.ok) { + throw new Error(`Failed to upload JSON: ${response.status} ${response.statusText}`); + } +} + +// Main function +async function main() { + try { + const jsonData = await readJson(args.values["json-file"]); + await uploadJson(args.values["api-url"], args.values["api-key"], jsonData); + console.log("JSON uploaded successfully."); + } catch (error) { + console.error("Error:", error.message); + process.exit(1); + } +} + +main();