1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#!/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"
|