#compdef _rest-run rest-run # zsh completion for rest-run -*- shell-script -*- function __debug { echo "$1" >> /tmp/_rest-run.log } function _rest-run_read_domain () { local ARG="${words[2]}" if [ ${#ARG} -lt 7 ]; then ARG="${words[3]}" fi echo "$ARG" | sed 's/[a-z]*:\/\///g' | sed 's/\/.*//g' | sed 's/\?.*//g' } function _rest-run { local line local -a args args+=( '1:*:(POST GET PUT PATCH DELETE)' '2:*:_rest-run_url_opts' '-ne[Skip request body edit]' '-b[Body options]: :_rest-run_body_opts' '-r[Response options]: :_rest-run_response_opts' '-e[Extract request data]: :_rest-run_extract_opts' '-rr[Read response file]: :_rest-run_select_response_opts' '*-h[HTTP header]' '-p[Paginate request response]' '--no-color[Disable ANSI color scape codes (default when output is being piped)]' '--color[Enable ANSI color scape codes]' ) _arguments -C $args } function _rest-run_url_opts { local -a _values _values=() tail -n -50 $HOME/.cache/rest-run/request-history 2> /dev/null | while read -r line; do _values+=("$(echo $line | sed 's/^[A-Z]*\s//g')") done compadd -a _values } function _rest-run_body_opts { local -a _descriptions _values _descriptions=( 'default -- create a request body using the default editor' 'last -- use the last request' 'history -- pick from the request history' 'stdin -- reads from piped input' ) _values=( 'default' 'last' 'history' 'stdin' ) compadd -d _descriptions -a _values } function _rest-run_response_opts { local -a _descriptions _values _descriptions=( 'default -- store the request on cache' 'last -- read last request, skip request' 'history -- pick from the response history, skip request' ) _values=( 'default' 'last' 'history' ) compadd -d _descriptions -a _values } function _rest-run_extract_opts { local -a _descriptions _values _descriptions=( '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' ) _values=( 'default' 'body' 'headers' 'rbody' 'rheaders' ) compadd -d _descriptions -a _values } function _rest-run_select_response_opts { local domain="$(_rest-run_read_domain)" local -a _values for f in $(find $HOME/.cache/rest-run/$domain -type f -iname '*response*.log' 2> /dev/null | head -50); do _values+=("$(echo "$f" | sed 's/^.*\/\.cache\/rest-run\///g')") done compadd -a _values } if [ "$funcstack[1]" = "_rest-run" ]; then _rest-run fi compdef _rest-run rest-run