Last active
August 29, 2015 13:57
-
-
Save Robertof/9717274 to your computer and use it in GitHub Desktop.
pasteling.sh - a Bash script for Pasteling. Please check out the help with `pasteling -h`.
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
#!/usr/bin/env bash | |
PL_VERSION="1.1.1" | |
PL_KEY="" | |
PL_LANG="" | |
PL_FULLPAGE="" | |
PL_URL="http://pasteling.giovannicapuano.net/api" | |
command -v curl >/dev/null 2>&1 || \ | |
{ echo >&2 "This script requires 'curl'. Aborting."; exit 2; } | |
command -v rev >/dev/null 2>&1 || \ | |
{ echo >&2 "This script requires 'rev'." \ | |
"Please install 'util-linux'. Aborting."; exit 2; } | |
while getopts ":k:l:hfL" opt; do | |
case $opt in | |
k) | |
PL_KEY="$OPTARG" | |
;; | |
l) | |
PL_LANG="$OPTARG" | |
;; | |
L) | |
echo "Here's the list of available langauges:" | |
curl -s "${PL_URL}/langs" | grep -Eo '"(name|mode)":[^,]*' | \ | |
cut -c 10- | rev | cut -c 2- | rev | while read PL_LANG; do | |
[[ -z "${PL_TMP}" ]] \ | |
&& { echo -n "${PL_LANG}"; PL_TMP="${PL_LANG,,}"; } \ | |
|| { [[ "${PL_TMP}" != "${PL_LANG}" ]] && \ | |
echo " (${PL_LANG})" || echo; unset PL_TMP; } | |
done | |
exit 1 | |
;; | |
f) | |
PL_FULLPAGE="yes" | |
;; | |
h) | |
echo -n "pasteling script" >&2 | |
echo " - Version: ${PL_VERSION}" >&2 | |
echo "Made by Roberto Frenna <https://github.com/Robertof>" >&2 | |
echo "Usage: $0 [-k key] [-l lang] [-f] {/path/to/file}" >&2 | |
echo "If you don't provide a path to a file, it will read from STDIN.">&2 | |
echo >&2 | |
echo "Available parameters:" >&2 | |
echo -e "\t-k key\tSets the key which is used to encrypt the paste." >&2 | |
echo -e "\t-l lang\tSets the language to use." >&2 | |
echo -e "\t-L\tShows a list of available languages." >&2 | |
echo -e "\t-f\tReturns the link of the full paste page." >&2 | |
exit 1 | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
[[ -n "$@" ]] && [[ ! -e "${@}" ]] && \ | |
{ echo >&2 "The file you provided does not exist."; exit 1; } | |
PL_TEXT="@${@}" | |
[[ -z "$@" ]] && PL_TEXT="$(< /dev/stdin)" | |
PL_CURLRES="$(curl -s -S -A "plscript/${PL_VERSION}" \ | |
-F "lang=${PL_LANG}" -F "key=${PL_KEY}" \ | |
-F "text=${PL_TEXT}" "${PL_URL}/new" 2>&1)" | |
PL_REQSTATUS="$(echo "$PL_CURLRES" | grep -o '"status":[^,]*')" | |
if [[ "${?}" -ne 0 ]] || [[ "${PL_REQSTATUS}" != '"status": "success"' ]]; then | |
echo >&2 "An error occurred while uploading the paste." | |
PL_MESSAGE="$(echo "$PL_CURLRES" | grep -o '"message"[^,]*,')" | |
[ $? -eq 0 ] \ | |
&& echo "$PL_MESSAGE" | cut -c13- | rev | cut -c3- | rev >&2 \ | |
|| echo >&2 "${PL_CURLRES}" | |
exit 3 | |
else | |
PL_FINAL_URL="$(echo "$PL_CURLRES" | grep -o "${PL_URL}[^\"]*")" | |
[[ -z "${PL_FULLPAGE}" ]] \ | |
&& PL_FINAL_URL="${PL_FINAL_URL/api/api/read}" \ | |
|| PL_FINAL_URL="${PL_FINAL_URL/api\/}" | |
echo $PL_FINAL_URL | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment