#!/bin/sh set -eu site=${GITPOCKET_URL:-https://gitpocket.io} visibility=secret description= name= usage() { echo "usage: gist [-p] [-d description] [-n name] [file ...]" >&2 echo "reads stdin when no file is given, GITPOCKET_TOKEN must hold a token scoped gist or all" >&2 exit 2 } enc() { printf %s "$1" | od -An -tx1 -v | tr -d ' \n' | sed 's/../%&/g' } while getopts pd:n:h opt; do case $opt in p) visibility=public ;; d) description=$OPTARG ;; n) name=$OPTARG ;; *) usage ;; esac done shift $((OPTIND - 1)) token=${GITPOCKET_TOKEN:-} [ -n "$token" ] || usage api="$site/api/gists" auth="Authorization: Bearer $token" if [ $# -eq 0 ]; then exec curl -sS --fail-with-body -H "$auth" --data-binary @- \ "$api?name=$(enc "$name")&visibility=$visibility&description=$(enc "$description")" fi first=$1 shift if ! url=$(curl -sS --fail-with-body -H "$auth" --data-binary @"$first" \ "$api?name=$(enc "${name:-$(basename "$first")}")&visibility=$visibility&description=$(enc "$description")"); then echo "$url" >&2 exit 1 fi id=${url##*/} for file in "$@"; do curl -sS --fail-with-body -o /dev/null -X PUT -H "$auth" --data-binary @"$file" \ "$api/$id/files/$(enc "$(basename "$file")")" done echo "$url"