feat(contrib): add a curl only gist uploader
1 files changed, +51 −0
- contrib/gist added +51 −0
contrib/gist added
| @@ -0,0 +1,51 @@ | ||
| 1 | +#!/bin/sh | |
| 2 | +set -eu | |
| 3 | + | |
| 4 | +site=${GITPOCKET_URL:-https://gitpocket.io} | |
| 5 | +visibility=secret | |
| 6 | +description= | |
| 7 | +name= | |
| 8 | + | |
| 9 | +usage() { | |
| 10 | + echo "usage: gist [-p] [-d description] [-n name] [file ...]" >&2 | |
| 11 | + echo "reads stdin when no file is given, GITPOCKET_TOKEN must hold a token scoped gist or all" >&2 | |
| 12 | + exit 2 | |
| 13 | +} | |
| 14 | + | |
| 15 | +enc() { | |
| 16 | + printf %s "$1" | od -An -tx1 -v | tr -d ' \n' | sed 's/../%&/g' | |
| 17 | +} | |
| 18 | + | |
| 19 | +while getopts pd:n:h opt; do | |
| 20 | + case $opt in | |
| 21 | + p) visibility=public ;; | |
| 22 | + d) description=$OPTARG ;; | |
| 23 | + n) name=$OPTARG ;; | |
| 24 | + *) usage ;; | |
| 25 | + esac | |
| 26 | +done | |
| 27 | +shift $((OPTIND - 1)) | |
| 28 | + | |
| 29 | +token=${GITPOCKET_TOKEN:-} | |
| 30 | +[ -n "$token" ] || usage | |
| 31 | +api="$site/api/gists" | |
| 32 | +auth="Authorization: Bearer $token" | |
| 33 | + | |
| 34 | +if [ $# -eq 0 ]; then | |
| 35 | + exec curl -sS --fail-with-body -H "$auth" --data-binary @- \ | |
| 36 | + "$api?name=$(enc "$name")&visibility=$visibility&description=$(enc "$description")" | |
| 37 | +fi | |
| 38 | + | |
| 39 | +first=$1 | |
| 40 | +shift | |
| 41 | +if ! url=$(curl -sS --fail-with-body -H "$auth" --data-binary @"$first" \ | |
| 42 | + "$api?name=$(enc "${name:-$(basename "$first")}")&visibility=$visibility&description=$(enc "$description")"); then | |
| 43 | + echo "$url" >&2 | |
| 44 | + exit 1 | |
| 45 | +fi | |
| 46 | +id=${url##*/} | |
| 47 | +for file in "$@"; do | |
| 48 | + curl -sS --fail-with-body -o /dev/null -X PUT -H "$auth" --data-binary @"$file" \ | |
| 49 | + "$api/$id/files/$(enc "$(basename "$file")")" | |
| 50 | +done | |
| 51 | +echo "$url" | |