From 36e24eeb08579316bb19cd6dd42c9c15d450e71b Mon Sep 17 00:00:00 2001 From: Joel Kuzmarski Date: Sat, 8 Feb 2014 15:51:51 -0600 Subject: [PATCH] git-hub-shorten-url: Slug support --- modules/git/functions/_git-hub-shorten-url | 5 ++++- modules/git/functions/git-hub-shorten-url | 20 ++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/modules/git/functions/_git-hub-shorten-url b/modules/git/functions/_git-hub-shorten-url index 32199ce..63665ae 100644 --- a/modules/git/functions/_git-hub-shorten-url +++ b/modules/git/functions/_git-hub-shorten-url @@ -5,8 +5,11 @@ # Completes git-hub-shorten-url. # # Authors: +# Joel Kuzmarski # Sorin Ionescu # -_arguments '1:url:' && return 0 +_arguments \ + '1:url:' \ + '2:slug:' && return 0 diff --git a/modules/git/functions/git-hub-shorten-url b/modules/git/functions/git-hub-shorten-url index c394f5b..921b832 100644 --- a/modules/git/functions/git-hub-shorten-url +++ b/modules/git/functions/git-hub-shorten-url @@ -2,21 +2,29 @@ # Shortens GitHub URLs. # # Authors: +# Joel Kuzmarski # Sorin Ionescu # local url="$1" -if [[ "$url" == '-' ]]; then - read url <&0 -fi - if [[ -z "$url" ]]; then - print "usage: $0 [ url | - ]" >&2 + print "usage: $0 [ url | - ] [slug]" >&2 + return 1 +elif [[ "$url" == '-' ]]; then + read url <&0 +elif [[ "$url" != *github.com* ]]; then + url="https://github.com/$url" fi if (( $+commands[curl] )); then - curl -s -i 'http://git.io' -F "url=$url" | grep 'Location:' | sed 's/Location: //' + local result="$(curl -s -i 'http://git.io' -F "url=$url" -F "code=$2")" + + if [[ $result == *Unprocessable* ]]; then + print "$0: the slug '$2' is already taken!" >&2 + else + print $result | grep 'Location:' | sed 's/Location: //' + fi else print "$0: command not found: curl" >&2 fi