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:
2026-01-21 20:48:28 +01:00
parent 7e78ef65b1
commit 710682d931
2 changed files with 170 additions and 10 deletions

View File

@@ -1,16 +1,57 @@
#!/bin/bash
# JMESPath Playground Upload Script
# Usage: ./upload.sh "json_file.json"
# Usage: ./upload.sh [-u URL] "json_file.json"
if [ $# -ne 1 ]; then
echo "Usage: $0 <json_file>"
echo "Example: $0 data.json"
show_usage() {
echo "Usage: $0 [-u|--url URL] <json_file>"
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
fi
shift
;;
esac
done
if [ -z "$JSON_FILE" ]; then
echo "Error: JSON file required"
show_usage
exit 1
fi
JSON_FILE="$1"
if [ ! -f "$JSON_FILE" ]; then
echo "Error: JSON file '$JSON_FILE' not found"
exit 1
@@ -24,9 +65,6 @@ if command -v jq >/dev/null 2>&1; then
fi
fi
JSON_DATA=$(cat "$JSON_FILE")
API_URL="${API_URL:-http://localhost:3000}"
echo "Uploading sample data to JMESPath Playground..."
echo "JSON file: $JSON_FILE"
echo "API URL: $API_URL"
@@ -35,7 +73,7 @@ echo
# Upload the JSON data
curl -s -X POST \
-H "Content-Type: application/json" \
-d "$JSON_DATA" \
--data @"$JSON_FILE" \
"$API_URL/api/v1/upload"
echo