Created
March 30, 2023 09:32
-
-
Save mozartilize/76f9bc37f70d54a5e09444a799b672bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -o errexit # abort on nonzero exitstatus | |
set -o pipefail # don't hide errors within pipes | |
# enable trace if the variable set | |
[[ "$TRACE" ]] && set -x | |
VALID_ARGS=$(getopt -o '' --long curl-opts:,jq-opts: -- "$@") | |
if [[ $? -ne 0 ]]; then | |
exit 1 | |
fi | |
curlOpts="" | |
jqOpts="" | |
eval set -- "$VALID_ARGS" | |
while [ : ]; do | |
case "$1" in | |
--curl-opts) | |
curlOpts=$2 | |
shift 2 | |
;; | |
--jq-opts) | |
jqOpts=$2 | |
shift 2 | |
;; | |
--) | |
shift | |
break | |
;; | |
esac | |
done | |
input_file=$(basename $1) | |
session_id=$(uuid) | |
set -a; source $ENV_FILE; source .env.local; set +a; | |
cat $1 | envsubst | curl -K - $curlOpts -o /tmp/curlie_out_$input_file-$session_id | |
if [[ $curlOpts == *"-i"* || $curlOpts == *"--include"* ]]; then | |
touch /tmp/curlie_body_$input_file-$session_id | |
moveBody=0 | |
while IFS= read -r CMD || [[ -n "$CMD" ]]; do | |
if [ $moveBody -eq "1" ]; then | |
printf '%s\n' "$CMD" >> /tmp/curlie_body_$input_file-$session_id | |
else | |
if [[ $CMD == *[^$'\r\n']* ]]; then | |
printf '%s\n' "$CMD" | |
else | |
moveBody=1 | |
fi | |
fi | |
done < /tmp/curlie_out_$input_file-$session_id | |
mv /tmp/curlie_body_$input_file-$session_id /tmp/curlie_out_$input_file-$session_id | |
fi | |
cat /tmp/curlie_out_$input_file-$session_id | jq $jqOpts | |
rm /tmp/curlie_out_$input_file-$session_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment