Enhance upload scripts with command-line options and JavaScript implementation
- Add -u/--url option to specify API URL via command line - Refactor usage messages into centralized show_usage() function - Remove environment variable fallback for API_URL - Use curl's native file handling with --data @file - Add JavaScript version (upload.js) with identical functionality - Both scripts support same command-line interface and options
This commit is contained in:
122
scripts/upload.js
Executable file
122
scripts/upload.js
Executable file
@@ -0,0 +1,122 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JMESPath Playground Upload Script (JavaScript)
|
||||||
|
* Usage: node upload.js [-u URL] "json_file.json"
|
||||||
|
*/
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
function showUsage() {
|
||||||
|
const scriptName = path.basename(process.argv[1]);
|
||||||
|
console.log(`Usage: node ${scriptName} [-u|--url URL] <json_file>`);
|
||||||
|
console.log('');
|
||||||
|
console.log('Options:');
|
||||||
|
console.log(' -u, --url URL API URL (default: http://localhost:3000)');
|
||||||
|
console.log(' -h, --help Show this help message');
|
||||||
|
console.log('');
|
||||||
|
console.log('Example:');
|
||||||
|
console.log(` node ${scriptName} data.json`);
|
||||||
|
console.log(` node ${scriptName} -u http://example.com:3000 data.json`);
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseArguments() {
|
||||||
|
const args = process.argv.slice(2);
|
||||||
|
let apiUrl = 'http://localhost:3000';
|
||||||
|
let jsonFile = '';
|
||||||
|
|
||||||
|
for (let i = 0; i < args.length; i++) {
|
||||||
|
const arg = args[i];
|
||||||
|
|
||||||
|
if (arg === '-u' || arg === '--url') {
|
||||||
|
if (i + 1 >= args.length) {
|
||||||
|
console.error('Error: URL argument required for -u/--url option');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
apiUrl = args[i + 1];
|
||||||
|
i++; // Skip next argument
|
||||||
|
} else if (arg === '-h' || arg === '--help') {
|
||||||
|
showUsage();
|
||||||
|
process.exit(0);
|
||||||
|
} else if (arg.startsWith('-')) {
|
||||||
|
console.error(`Error: Unknown option ${arg}`);
|
||||||
|
showUsage();
|
||||||
|
process.exit(1);
|
||||||
|
} else {
|
||||||
|
if (jsonFile) {
|
||||||
|
console.error('Error: Multiple JSON files specified');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
jsonFile = arg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!jsonFile) {
|
||||||
|
console.error('Error: JSON file required');
|
||||||
|
showUsage();
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { apiUrl, jsonFile };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function validateJsonFile(jsonFile) {
|
||||||
|
// Check if file exists
|
||||||
|
if (!fs.existsSync(jsonFile)) {
|
||||||
|
console.error(`Error: JSON file '${jsonFile}' not found`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate JSON content
|
||||||
|
try {
|
||||||
|
const content = fs.readFileSync(jsonFile, 'utf8');
|
||||||
|
JSON.parse(content);
|
||||||
|
return content;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error: '${jsonFile}' contains invalid JSON`);
|
||||||
|
console.error(error.message);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function uploadData(apiUrl, jsonFile, jsonData) {
|
||||||
|
console.log('Uploading sample data to JMESPath Playground...');
|
||||||
|
console.log(`JSON file: ${jsonFile}`);
|
||||||
|
console.log(`API URL: ${apiUrl}`);
|
||||||
|
console.log('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${apiUrl}/api/v1/upload`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: jsonData
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Sample data uploaded successfully!');
|
||||||
|
console.log(`Open ${apiUrl} in your browser to see the reload button.`);
|
||||||
|
console.log('You can then enter your JMESPath expression in the web interface.');
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error uploading data:', error.message);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const { apiUrl, jsonFile } = parseArguments();
|
||||||
|
const jsonData = await validateJsonFile(jsonFile);
|
||||||
|
await uploadData(apiUrl, jsonFile, jsonData);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run the script
|
||||||
|
main().catch((error) => {
|
||||||
|
console.error('Unexpected error:', error);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
@@ -1,15 +1,56 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# JMESPath Playground Upload Script
|
# JMESPath Playground Upload Script
|
||||||
# Usage: ./upload.sh "json_file.json"
|
# Usage: ./upload.sh [-u URL] "json_file.json"
|
||||||
|
|
||||||
if [ $# -ne 1 ]; then
|
show_usage() {
|
||||||
echo "Usage: $0 <json_file>"
|
echo "Usage: $0 [-u|--url URL] <json_file>"
|
||||||
echo "Example: $0 data.json"
|
echo ""
|
||||||
|
echo "Options:"
|
||||||
|
echo " -u, --url URL API URL (default: http://localhost:3000)"
|
||||||
|
echo " -h, --help Show this help message"
|
||||||
|
echo ""
|
||||||
|
echo "Example:"
|
||||||
|
echo " $0 data.json"
|
||||||
|
echo " $0 -u http://example.com:3000 data.json"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Parse command line options
|
||||||
|
API_URL="http://localhost:3000"
|
||||||
|
JSON_FILE=""
|
||||||
|
|
||||||
|
while [[ $# -gt 0 ]]; do
|
||||||
|
case $1 in
|
||||||
|
-u|--url)
|
||||||
|
API_URL="$2"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
-h|--help)
|
||||||
|
show_usage
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-*)
|
||||||
|
echo "Error: Unknown option $1"
|
||||||
|
show_usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
if [ -z "$JSON_FILE" ]; then
|
||||||
|
JSON_FILE="$1"
|
||||||
|
else
|
||||||
|
echo "Error: Multiple JSON files specified"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
JSON_FILE="$1"
|
if [ -z "$JSON_FILE" ]; then
|
||||||
|
echo "Error: JSON file required"
|
||||||
|
show_usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
if [ ! -f "$JSON_FILE" ]; then
|
if [ ! -f "$JSON_FILE" ]; then
|
||||||
echo "Error: JSON file '$JSON_FILE' not found"
|
echo "Error: JSON file '$JSON_FILE' not found"
|
||||||
@@ -24,9 +65,6 @@ if command -v jq >/dev/null 2>&1; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
JSON_DATA=$(cat "$JSON_FILE")
|
|
||||||
API_URL="${API_URL:-http://localhost:3000}"
|
|
||||||
|
|
||||||
echo "Uploading sample data to JMESPath Playground..."
|
echo "Uploading sample data to JMESPath Playground..."
|
||||||
echo "JSON file: $JSON_FILE"
|
echo "JSON file: $JSON_FILE"
|
||||||
echo "API URL: $API_URL"
|
echo "API URL: $API_URL"
|
||||||
@@ -35,7 +73,7 @@ echo
|
|||||||
# Upload the JSON data
|
# Upload the JSON data
|
||||||
curl -s -X POST \
|
curl -s -X POST \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "$JSON_DATA" \
|
--data @"$JSON_FILE" \
|
||||||
"$API_URL/api/v1/upload"
|
"$API_URL/api/v1/upload"
|
||||||
|
|
||||||
echo
|
echo
|
||||||
|
|||||||
Reference in New Issue
Block a user