Created
January 14, 2011 16:23
-
-
Save millenomi/779820 to your computer and use it in GitHub Desktop.
This is a wrapper around the 'curl' command-line tool that adds a few useful features.
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 | |
if [ "$#" == "0" ]; then | |
echo "usage: $0 [CURL FLAGS|--json FILE|--https|--dry-run]* URL" >&2 | |
echo "You can set the BASE_URL environment variable; if you do, all relative URLs will be have the BASE_URL appended in front of them. (Note that this appends blindly the two strings together, doing no resolution work.)" >&2 | |
exit 1 | |
fi | |
args=("$@") | |
verbatim_args=() | |
((last_arg = $# - 1)) | |
((j = 0)) | |
for (( i = 0; i < $# - 1; i++ )); do | |
((j++)) | |
if [ "${args[$i]}" == "--json" ]; then | |
verbatim_args[$j]="--data-binary" | |
verbatim_args[$j + 1]=@"${args[$i + 1]}" | |
verbatim_args[$j + 2]="-H" | |
verbatim_args[$j + 3]="Content-Type: application/json" | |
(( j += 3 )) | |
(( i++ )) | |
elif [ "${args[$i]}" == "--dry-run" ]; then | |
ILABS_DRY_RUN=YES | |
elif [ "${args[$i]}" == "--https" ]; then | |
echo "Will use HTTPS if the final URL expands to '...http://...'." | |
turn_http_to_https=YES | |
else | |
verbatim_args[$j]="${args[$i]}" | |
fi | |
done | |
url="${args[$last_arg]}" | |
if [[ ! "$url" =~ "://" ]]; then | |
url="$BASE_URL""${args[$last_arg]}" | |
fi | |
if [[ "$turn_http_to_https" == "YES" && "$url" =~ "http://" ]]; then | |
url="https://${url:7}" | |
echo " => Using HTTPS url: $url" | |
fi | |
if [ "$ILABS_CREDENTIALS" != "" ]; then | |
credentials=("-u" "$ILABS_CREDENTIALS") | |
else | |
credentials=() | |
fi | |
if [ "$ILABS_DRY_RUN" == "YES" ]; then | |
echo curl -D- "${verbatim_args[@]}" "${credentials[@]}" "$url" | |
else | |
set -ax | |
curl -D- "${verbatim_args[@]}" "${credentials[@]}" "$url" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment