Created
September 12, 2015 19:19
-
-
Save nfarrar/50a2787a12ee21dd13ef to your computer and use it in GitHub Desktop.
bash script for shortening urls with git.io
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
#!/usr/bin/env bash | |
# Author: Nathan Farrar <[email protected]> | |
# Website: https://crunk.io | |
# Description: Command-line git.io shortner script, in bash. | |
# | |
#/ Usage: gitio [options] <url> | |
#/ Shortens github URLs using git.io. The generated URL is automatically | |
#/ copied to the clipboard (requires xclip on linux platforms). | |
#/ -c <code> Specify an optional code to be used as a 'vanity' URL. | |
#/ -v Display verbose messages. | |
#/ -h Display this message & exit. | |
# Required to trim whitespace when parsing response. | |
shopt -s extglob | |
# The url for the git.io api service. | |
api_url="http://git.io" | |
# Output wrappers. | |
msg() { printf '\x1b[90m%s\x1b[0m\n' "$1" >&2; } | |
dbg() { [[ $dbg -eq 1 ]] && printf '\x1b[35mDEBUG:\t%s\x1b[0m\n' "$1" >&2; } | |
err() { printf '\n\x1b[31mERROR:\t%s\x1b[0m\n' "$1" >&2; } | |
# Display the usage message. | |
usage() { | |
while IFS='\n' read -r line ; do | |
msg "$line" | |
done < <(grep '^#/' <"$0" | cut -c4-) | |
} | |
# If no arguments were provided, display the usage message and exit. | |
[[ $# -eq 0 ]] && { usage; exit 0; }; | |
# Parse options using getopts. | |
# The leading ':' tells getopts to suppress error messages (they're handled | |
# by the getopts loop. The ':' after 'c' tells getopts that 'c' has a required | |
# argument. | |
while getopts ":hdc:" option; do | |
case "${option}" in | |
h) usage;exit 0;; | |
d) dbg=1;; | |
c) code=$OPTARG;; | |
\?) usage; err "Unknown option: -$OPTARG" >&2; exit 1;; | |
:) usage; err "Missing option argument for -$OPTARG" >&2; exit 1;; | |
*) usage; err "Unimplemented option: -$OPTARG" >&2; exit 1;; | |
esac | |
done | |
# Strip out the processed options, leaving the remaining command line | |
# arguments. The only argument left *should* be the URL to shorten. | |
shift "$((OPTIND - 1))" | |
[[ $# -ne 1 ]] && { usage; err "URL not provided."; exit 1; } | |
url="$1" | |
cmd="curl -s -i $api_url -F url=$url ${code:+-F code=$code}" | |
dbg "api_url=$api_url" | |
dbg "url=$url" | |
dbg "code=$code" | |
dbg "cmd=$cmd" | |
# If debugging output is enabled, insert an extra newline before it. | |
[[ $dbg -eq 1 ]] && printf "\n" >&2 | |
# Execute the curl command, and parse the response values as key:value pairs. | |
# The output from the command is tee'd to stderr, so that we can see the | |
# response and still capture the output. | |
while IFS=':' read key value; do | |
value=${value##+([[:space:]])}; value=${value%%+([[:space:]])} | |
case "$key" in | |
Status) status="$value";; | |
Location) location="$value";; | |
HTTP*) read PROTO STATUS MSG <<< "$key{$value:+:$value}";; | |
esac | |
done < <($cmd | tee >(cat >&2) ) | |
# If debugging output is enabled, insert an extra newline before it. | |
[[ $dbg -eq 1 ]] && printf "\n" >&2 | |
dbg "status=$status" | |
dbg "location=$location" | |
printf "The URL $url has been shortened to $location.\n" | |
# Copy the location to the clipboard. | |
if [[ "$OSTYPE" == "darwin"* ]]; then | |
printf "$location" | pbcopy | |
dbg "copied $location to clipboard" | |
elif [[ $OSTYPE == 'linux-gnu' ]] && [[ -x $(command -v xclip) ]]; then | |
printf "$location" | xclip -selection clipboard | |
dbg "copied $location to clipboard" | |
elif [[ $OSTYPE == "cygwin" ]]; then | |
printf "$location" | /dev/clipboard | |
dbg "copied $location to clipboard" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment