Skip to content

Instantly share code, notes, and snippets.

@Akaricchi
Created July 25, 2011 09:14
Show Gist options
  • Select an option

  • Save Akaricchi/1103807 to your computer and use it in GitHub Desktop.

Select an option

Save Akaricchi/1103807 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
usage() {
cat << _EOF_
usage: $_app [option] <file|code>
options:
-p create a private gist
-g get gist specified by its code
-c clone gist specified by its code
-h print this help message
_EOF_
}
perror() {
printf "%s: %s\n" "$_app" "$@" >&2
exit 1
}
gist() {
[[ -f $1 ]] || perror "not a file: $1"
local gisturl='https://gist.github.com/gists'
local user token filename fileext response
filename="$1"
user="$(git config --global github.user)" || perror "no github user set"
token="$(git config --global github.token)" || perror "no github token set"
fileext="${filename##*.}"
[[ $filename == $fileext ]] && fileext="txt"
gistit="curl --include --silent \
--data-urlencode private$private \
--data-urlencode login=$user --data-urlencode token=$token \
--data-urlencode file_name[gistfile1]=$filename \
--data-urlencode file_ext[gistfile1]=$fileext \
--data-urlencode file_contents[gistfile1]@$filename $gisturl"
response="$(sed -nr '/Location/{s/.+: (.+)/\1/p}' < <($gistit))" || perror "failed to gistit"
xclip -i <<< "$response"
printf "%s: %s\n" "gisted at" "$response"
}
private() {
local private="=on"
gist "$@"
}
get() {
local file="$1" gisturl='https://gist.github.com'
while [[ -f "$file.txt" ]]
do file="$file-$RANDOM"
done
curl --silent "$gisturl/$1.txt" > "$file.txt" || perror "failed to fetch file"
xclip -i <<< "$PWD/$file.txt"
printf "%s: %s\n" "gist stored at" "$file.txt"
}
clone() {
local dir="$1" gisturl='git://gist.github.com'
while [[ -d $dir ]]
do dir="$1-$RANDOM"
done
git clone "git://gist.github.com/$1.git" "$dir" || perror "failed to clone gist"
xclip -i <<< "$PWD/$dir"
printf "%s: %s\n" "gist cloned at" "$dir"
}
_app="${0##*/}"
which git &>/dev/null || perror "missing dependency: git"
which curl &>/dev/null || perror "missing dependency: curl"
which xclip &>/dev/null || perror "missing dependency: xclip"
(( $# > 2 )) && perror "too many arguments"
while (( $# )); do
case $1 in
-h) usage
exit 0
;;
-p) shift
private "$@"
exit 0
;;
-c) shift
clone "$@"
exit 0
;;
-g) shift
get "$@"
exit 0
;;
-*) usage
exit 1
;;
*) gist "$@"
exit 0
;;
esac
done
usage
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment