Refactor upload script to use environment variables for API URL and key, and update usage instructions

This commit is contained in:
2026-01-26 16:34:52 +01:00
parent 03cc889cd0
commit 37c73ddd2b

View File

@@ -2,23 +2,33 @@
set -euo pipefail set -euo pipefail
API_URL="https://jmespath-playground.koszewscy.waw.pl" JMESPATH_PLAYGROUND_API_URL="http://localhost:3000" # May be set in bash profile
JMESPATH_PLAYGROUND_API_KEY="" # Required if not localhost
JSON_FILE="-" JSON_FILE="-"
ADD_HEADERS=()
function usage() { function usage() {
echo "Usage: $0 [--api-url <url>] [--json-file <file>]" echo "Usage: $0 [--api-url <url>] [--json-file <file>]"
exit 1 echo
echo "Options:"
echo " --api-url <url> The base URL of the JMESPath Playground API (default: http://localhost:3000)"
echo " --api-key <key> The API key for authentication (required if not localhost)"
echo " --json-file <file> The JSON file to upload (default: stdin if not specified)"
echo " -h, --help Show this help message and exit"
echo
echo "Environment Variables:"
echo " JMESPATH_PLAYGROUND_API_URL Can be used to set the API URL"
echo " JMESPATH_PLAYGROUND_API_KEY Can be used to set the API key"
} }
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
--api-url) --api-url)
API_URL="$2" JMESPATH_PLAYGROUND_API_URL="$2"
shift 2 shift 2
;; ;;
--api-key) --api-key)
ADD_HEADERS+=("-H" "X-API-Key: $2") JMESPATH_PLAYGROUND_API_KEY="$2"
shift 2 shift 2
;; ;;
--json-file) --json-file)
@@ -37,6 +47,8 @@ while [[ $# -gt 0 ]]; do
esac esac
done done
ADD_HEADERS+=("-H" "X-API-Key: $JMESPATH_PLAYGROUND_API_KEY")
# Send the POST request # Send the POST request
curl -s -X POST \ curl -s -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
@@ -44,4 +56,3 @@ curl -s -X POST \
"${ADD_HEADERS[@]}" \ "${ADD_HEADERS[@]}" \
--data @${JSON_FILE} \ --data @${JSON_FILE} \
"$API_URL/api/v1/upload" "$API_URL/api/v1/upload"