Last active
January 11, 2022 15:13
-
-
Save lexisother/ce8655fe6ae9bccec45e03ad003ee62d to your computer and use it in GitHub Desktop.
A simple tool to upload files to imperialb.in
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 | |
# TODO: Implement autodetection of the code language using Linguist | |
# The one reason I am slightly opposed to implementing this is because, well, | |
# it requires me to rely on an external tool. Usually I would not have many | |
# problems with this, but it's a shell script, I prefer to have it as | |
# self-contained as can be. See, if I were to use external tools to power the | |
# script such as Linguist, I might as well just write the thing in Ruby and | |
# call it a day. | |
# Utility functions {{{ | |
# Logging {{{ | |
ansi_reset="$(tput sgr0 || true)" | |
ansi_red="$(tput setaf 1 || true)" | |
ansi_yellow="$(tput setaf 3 || true)" | |
ansi_blue="$(tput setaf 4 || true)" | |
ansi_green="$(tput setaf 2 || true)" | |
log_info() { echo >&2 "${ansi_blue}[info]${ansi_reset}" "$@"; } | |
log_warn() { echo >&2 "${ansi_yellow}[warn]${ansi_reset}" "$@"; } | |
log_error() { echo >&2 "${ansi_red}[ERROR]${ansi_reset}" "$@"; } | |
log_success() { echo >&2 "${ansi_green}[success]${ansi_reset}" "$@"; } | |
# Use the log_error function to indicate where an error occurs | |
trap 'log_error line $LINENO' ERR | |
# }}} | |
# Checks for command on path | |
command_exists() { | |
whence -- "$@" &> /dev/null | |
} | |
# }}} | |
# Preliminary checks {{{ | |
if command_exists curl; then | |
log_error "cURL not found!" | |
log_error "Please check if you have installed cURL on your system." | |
exit 1 | |
fi | |
if command_exists jq; then | |
log_error "jq not found!" | |
log_error "Please check if you have installed jq on your system." | |
exit 1 | |
fi | |
# }}} | |
NETWORK_TIMEOUT=10 | |
HOST="https://imperialb.in/api/document" | |
# A wrapper around curl to avoid repetition | |
curl () { | |
command curl --location --fail --max-time "$NETWORK_TIMEOUT" "$@" | |
} | |
usage() { | |
if [ -z $1 ] || [ $1 == "version" ]; then | |
echo -e "Imperial v1.0" | |
if ! [ -z $1 ]; then exit; fi | |
echo "An imperialb.in implementation in Bash" | |
echo "" | |
elif [ $1 != "\r" ]; then | |
echo -e $1 | |
fi | |
echo -e "Usage: imperial <command> [arguments]\n" | |
echo "Commands:" | |
echo " upload - Uploads a file to imperialb.in" | |
} | |
if [ $# -eq 0 ]; then | |
usage | |
elif [ $1 == "usage" ] || [ $1 == "--usage" ] || [ $1 == "-h" ] || [ $1 == "--help" ]; then | |
usage | |
elif [ $1 == "version" ] || [ $1 == "-v" ] || [ $1 == "--version" ]; then | |
usage "version" | |
fi | |
case "$1" in | |
upload) | |
# No arguments? | |
if [ $# -eq 1 ]; then | |
echo "Usage: imperial upload <filename> - Uploads files to imperialb.in" | |
exit 1 | |
fi | |
filename="$2" | |
# TODO: Maybe bring back language selection? {{{ | |
# I don't know why you'd do this, as Imperial handles the language | |
# detection for us. | |
# if [ -z $3 ]; then | |
# log_info "No language name provided, using 'plaintext'" | |
# language="plaintext" | |
# else | |
# language="$3" | |
# fi | |
# }}} | |
jsonbody="{}" | |
jqdata=$(echo '{}' | jq --argfile filecontent <(jq --raw-input --slurp . $filename) '.code = $filecontent') | |
# If the file exists or is a symlink, upload | |
if [ -f "$filename" ] || [ -L "$filename" ]; then | |
name=$(basename -- "$filename") | |
log_info "Uploading $name" | |
retdata=`curl -H "Content-Type: application/json" -X POST -d "$jqdata" $HOST` | |
# TODO: Let the user configure the frontend URL | |
hissurl="https://snakeb.in/#" | |
fileid=`echo $retdata | jq --raw-output '.document.documentId'` | |
hissurl+=$fileid | |
langid=`echo $retdata | jq --raw-output '.document.language'` | |
hissurl+=".$langid" | |
log_success "File uploaded!" | |
echo "Your file URL: $hissurl" | |
else | |
log_error "File '$filename' does not exist!" | |
fi | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment