aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJefferson Julio <[email protected]>2022-05-15 17:31:10 -0300
committerJefferson Julio <[email protected]>2022-05-15 17:31:10 -0300
commit339b18db1b5f1d45367640221753bd4085909070 (patch)
tree9d94cbf4bdbb7543043b407e3e894ccff7d5783a
parent9021cca34ba5dcd239da6e211bbfef46bcdd4c85 (diff)
downloadrest-run-339b18db1b5f1d45367640221753bd4085909070.tar.bz2
rest-run-339b18db1b5f1d45367640221753bd4085909070.zip
Choose request part to print. Piping-aware options.
-rwxr-xr-xrest-run139
1 files changed, 112 insertions, 27 deletions
diff --git a/rest-run b/rest-run
index b0f855b..7ee4161 100755
--- a/rest-run
+++ b/rest-run
@@ -39,6 +39,15 @@ Options:
-ne|--no-edit Skip request editting. Ignored when --body is set to default
-h|--header Header string. Ex: "Authorization: Bearer 123"
-p|--paginate Paginate request response
+ -e|--extract Only output one part of the response or request, must be one of the following options:
+ default - whole request and response, headers and body
+ body - only response body (default when output is piped to another process)
+ headers - only response headers
+ rbody - only request body
+ rheaders - only request headers
+ --no-metadata Hide request metadata. For now, only "Elapsed time" is hidden by this option.
+ --no-color Disable ANSI color scape codes. Default when output is piped to another process.
+ --color Enable ANSI color scape codes, even when output is piped to another process.
rest-run parses your request body with a simple template engine called esh.
You can embed shell scripts/commands in your request file surrouning it
@@ -58,12 +67,17 @@ fail () {
exit 1
}
+fail-no-help () {
+ echo "$1" >&2
+ exit 1
+}
+
opt-fail () {
fail "Argument for $1 is missing"
}
opt-validation-fail () {
- fail "Value for option $1 is invalid"
+ fail-no-help "Value for option $1 is invalid"
}
extract-url-domain () {
@@ -74,6 +88,30 @@ remove-newlines-from-templates () {
sed 's/%>/ | xargs echo -n %>/g'
}
+remove-ansi-color-codes () {
+ sed -e 's/\x1b\[[0-9;]*m//g'
+}
+
+remove-http-metadata () {
+ head -n -2
+}
+
+extract-http-request-headers () {
+ sed '1,/^\r$/!d' | head -n -1
+}
+
+extract-http-response-headers () {
+ sed -E '/^\x1b?[\[0-9a-z]+HTTP/,/^\r$/!d' | head -n -1
+}
+
+extract-http-request-body () {
+ sed '1,/^\r$/d' | sed -E '/^\x1b?[\[0-9a-z]+HTTP/,$d' | head -n -2
+}
+
+extract-http-response-body () {
+ sed '1,/^\r$/d' | sed '1,/^\r$/d' | head -n -2
+}
+
# ] <- end helper functions
# [ Check installed dependencies
@@ -117,7 +155,15 @@ is_new_request=true
edit_rest_request=true
paginate_response=false
ignore_body=false
+rest_output_extract=default
+enable_color=true
+force_enable_color=false
+enable_metadata=true
+rest_output_extract_options=("default" "body" "headers" "rbody" "rheaders")
+httpie_print_options="HBhbm"
+
declare -a rest_headers=()
+declare -a output_pipeline=()
# ] <- end variable definition
@@ -127,11 +173,15 @@ while (( "$#" )); do
case "$1" in
-ne|--no-edit) edit_rest_request=false && shift;;
-p|--paginate) paginate_response=true && shift;;
+ --color) enable_color=true && force_enable_color=true && shift;;
+ --no-color) enable_color=false && shift;;
+ --no-metadata) enable_metadata=false && shift;;
--help) print-help && exit;;
-b|--body) [[ -n "$2" && ${2:0:1} != "-" ]] && rest_body=$2 && shift 2 || opt-fail $1 ;;
-r|--response) [[ -n "$2" && ${2:0:1} != "-" ]] && rest_response=$2 && shift 2 || opt-fail $1 ;;
-h|--header) [[ -n "$2" && ${2:0:1} != "-" ]] && rest_headers+=("$2") && shift 2 || opt-fail $1 ;;
-x|--method) [[ -n "$2" && ${2:0:1} != "-" ]] && rest_method="$2" && shift 2 || opt-fail $1 ;;
+ -e|--extract) [[ -n "$2" && ${2:0:1} != "-" ]] && rest_output_extract="$2" && shift 2 || opt-fail $1 ;;
-*|--*=) fail "Unkown option $1" ;;
*) PARAMS="$PARAMS $1" && shift ;;
esac
@@ -147,6 +197,7 @@ IFS=' ' read -r -a ARGS <<< "$PARAMS"
! [[ " ${rest_body_options[*]} " =~ "$rest_body" ]] && opt-validation-fail "-b|--body"
! [[ " ${rest_response_options[*]} " =~ "$rest_response" ]] && opt-validation-fail "-r|--response"
+! [[ " ${rest_output_extract_options[*]} " =~ "$rest_output_extract" ]] && opt-validation-fail "-e|--extract"
if [[ ${#ARGS[@]} -gt 1 ]]; then
rest_method="${ARGS[0]}"
@@ -159,7 +210,7 @@ case "$rest_method" in
PUT|put);;
PATCH|patch);;
DELETE|delete);;
- *) fail "Invalid HTTP Verb $rest_method" ;;
+ *) fail-no-help "Invalid HTTP Verb $rest_method" ;;
esac
[[ -z "$PARAMS" || "${#PARAMS}" = "1" ]] && fail "Not enough params"
@@ -185,24 +236,65 @@ if [[ "$rest_method" = "GET" || "$rest_method" = "get" ]]; then
ignore_body=true
fi
+if [ "$rest_body" = "default" ]; then
+ edit_rest_request=true
+fi
+
+if ! [ -t 1 ]; then
+ edit_rest_request=false
+ [ $force_enable_color = false ] && enable_color=false
+fi
+
# ] <- end arg validation
# [ rest-run!
+output-response () {
+
+ case "$rest_output_extract" in
+ body)
+ output_pipeline+=("| extract-http-response-body")
+ ;;
+ headers)
+ output_pipeline+=("| extract-http-response-headers")
+ ;;
+ rbody)
+ output_pipeline+=("| extract-http-request-body")
+ ;;
+ rheaders)
+ output_pipeline+=("| extract-http-request-headers")
+ ;;
+ default)
+ if [ $enable_metadata = false ]; then
+ output_pipeline+=("| remove-http-metadata")
+ fi
+ ;;
+ esac
+
+ if [ $enable_color = false ]; then
+ output_pipeline+=("| remove-ansi-color-codes")
+ fi
+
+ if [ $paginate_response = true ]; then
+ output_pipeline+=("| $PAGER")
+ fi
+
+ eval -- cat "$rest_response_file" "${output_pipeline[*]}"
+}
+
case "$rest_response" in
last)
- [ $paginate_response = true ] \
- && find "$CACHE_DIR_CONTEXT_RESPONSE" | sort -n | tail -1 | xargs $PAGER \
- || find "$CACHE_DIR_CONTEXT_RESPONSE" | sort -n | tail -1 | xargs cat
+ rest_response_file="$(find "$CACHE_DIR_CONTEXT_RESPONSE" | sort -n | tail -1)"
+ [[ "$(cat "$rest_response_file" 2> /dev/null)" = "" ]] && fail-no-help "No requests previously made"
+ output-response
exit
;;
history)
tmp_file="/tmp/choose-file-$$"
ranger --choosefile "$tmp_file" "$CACHE_DIR_CONTEXT_RESPONSE"
[[ "$(cat "$tmp_file" 2> /dev/null)" = "" ]] && fail "No file selected"
- [ $paginate_response = true ] \
- && cat "$(cat "$tmp_file")" | $PAGER \
- || cat "$(cat "$tmp_file")"
+ rest_response_file="$(cat "$tmp_file")"
+ output-response
rm "$tmp_file"
exit
;;
@@ -211,11 +303,11 @@ esac
if [ $ignore_body = false ]; then
case "$rest_body" in
last)
- [[ -z "$last_request_file" || "${#last_request_file}" = "0" ]] && fail "No requests previously made"
+ [[ -z "$last_request_file" || "${#last_request_file}" = "0" ]] && fail-no-help "No requests previously made"
cp "$(find "$CACHE_DIR_CONTEXT_REQUEST" | sort -n | tail -1)" "$rest_request_file"
;;
history)
- [[ -z "$last_request_file" || "${#last_request_file}" = "0" ]] && fail "No requests previously made"
+ [[ -z "$last_request_file" || "${#last_request_file}" = "0" ]] && fail-no-help "No requests previously made"
tmp_file="/tmp/choose-file-$$"
ranger --choosefile "$tmp_file" "$CACHE_DIR_CONTEXT_REQUEST"
[[ "$(cat "$tmp_file" 2> /dev/null)" = "" ]] && fail "No file selected"
@@ -227,7 +319,7 @@ if [ $ignore_body = false ]; then
;;
esac
- if [[ $edit_rest_request = true || "$rest_body" = "default" ]]; then
+ if [[ $edit_rest_request = true ]]; then
$EDITOR "$rest_request_file"
fi
@@ -235,16 +327,13 @@ fi
if [ $ignore_body = false ]; then # Send request body
- cat "$rest_request_file" \
- | remove-newlines-from-templates \
- | esh - \
- | eval -- http -p HBhbm --pretty=all \
- "$rest_method" \"$rest_url\" $(for h in "${rest_headers[@]}"; do echo \"$h\"; done) -o "$rest_response_file"
-
- [ $paginate_response = true ] \
- && $PAGER "$rest_response_file" \
- || cat "$rest_response_file"
+ cat "$rest_request_file" \
+ | remove-newlines-from-templates \
+ | esh - \
+ | eval -- http -p "$httpie_print_options" --pretty=all \
+ "$rest_method" \"$rest_url\" $(for h in "${rest_headers[@]}"; do echo \"$h\"; done) -o "$rest_response_file"
+ output-response
current_request_checksum="$(cat "$rest_request_file" | sha1sum | awk '{print $1}')"
if [[ "${#current_request_checksum}" != "0" && "${#current_request_checksum}" != "1" ]]; then
@@ -254,14 +343,10 @@ if [ $ignore_body = false ]; then # Send request body
fi
else # Send request without body
+ eval -- http --pretty=all -p "$httpie_print_options" \
+ "$rest_method" \"$rest_url\" $(for h in "${rest_headers[@]}"; do echo \"$h\"; done) -o "$rest_response_file"
- eval -- http --pretty=all -p HBhbm \
- "$rest_method" \"$rest_url\" $(for h in "${rest_headers[@]}"; do echo \"$h\"; done) -o "$rest_response_file"
-
- [ $paginate_response = true ] \
- && $PAGER "$rest_response_file" \
- || cat "$rest_response_file"
-
+ output-response
fi
# ] <- script end