Skip to content

Instantly share code, notes, and snippets.

Created December 22, 2014 15:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/99fb5b454d0dec3cf816 to your computer and use it in GitHub Desktop.
Save anonymous/99fb5b454d0dec3cf816 to your computer and use it in GitHub Desktop.
gisty!
#!/bin/bash
public=true
desc="(none)"
for i; do
case "$i" in
-p)
public=false
shift
;;
-d)
desc="$2"
shift; shift
;;
-u)
github_user="$2"
shift; shift
;;
*)
break
esac
done
escape_json() {
sed -e 's_\\_\\\\_g' \
-e 's/$/\\n/' \
-e 's/"/\\"/g' \
-e s/$'\t'/'\\t'/g \
| tr -d "\n"
}
gist_as_json() {
desc="$1"
public="$2"
shift; shift
files_added=0
echo "{"
printf "\"description\": \"%s\",\n" "$(escape_json <<<"$desc")"
printf "\"public\": %s,\n" "$public"
echo "\"files\": {"
for f; do # in "$@"
if ((files_added > 0)); then
printf ", "
fi
printf "\"%s\": { \"content\": \"%s\" }\n" "$f" "$(escape_json <"$f")"
files_added="$((files_added + 1))"
done
echo "} }"
}
gist_as_json "$desc" "$public" "$@" \
| curl --silent -H "Content-type: application/json" -d @- https://api.github.com/gists \
| sed -n '/html_url/ {;s/^.* //;s/"//g;s/,$//;p;q;}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment