Created
April 2, 2014 21:32
-
-
Save coderofsalvation/9943621 to your computer and use it in GitHub Desktop.
restconsole, a simple curl REST api wrapper for bash
This file contains hidden or 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 | |
# author: Leon van Kammen / Coder of Salvation 2014 | |
# | |
# restconsole, a simple curl REST api wrapper for bash | |
rooturl="http://api.yourcompany.com" | |
apikey="e270f99745341a89e883c583a25b821c" | |
cache=() | |
# here you can define your preset calls | |
# the variables can be insert directly in the console ('foo=flop' e.g.) | |
cache+=('POST /foo/bar?key=$apikey&flop=$flop') | |
cache+=('PUT /foo/bar?key=$apikey&foo=$foo') | |
# lets teach some history | |
teach(){ | |
for line in "${cache[@]}"; do | |
history -s -- "$line" | |
done | |
} | |
docurl(){ | |
line="$1"; line="${line// \// $rooturl/}"; line="$(echo "$line" | sed 's/&/\\&/g' )" | |
line="$(eval "echo "$line"")" | |
[[ ! "$line" == ^GET ]] && args="${line//*\?/}" && line="${line//\?*/}" | |
echo "=> $line" | |
echo "=> $args" | |
curl --data-urlencode "$args" -X ${line}; printf "\n\n"; | |
} | |
process(){ | |
[[ "$1" =~ ^POST ]] || [[ "$1" =~ ^GET ]] || [[ "$1" =~ ^PUT ]] || [[ "$1" =~ "DELETE" ]] && { | |
docurl "$line" | |
return 0; | |
} | |
[[ "$1" == "exit" ]] || [[ "$1" == "quit" ]] && exit 0 | |
eval "$line" # just allow inline bashscript | |
teach | |
} | |
subconsole(){ | |
echo -e "type 'exit' to quit\n" | |
while IFS="" read -r -e -d $'\n' -p "$2> " line; do | |
"$1" "$line" | |
# history -s "$line" # lets not add performed command to history | |
done | |
} | |
echo -e "\npress arrow up to cycle suggestions\n" | |
teach; subconsole process "$rooturl" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment