-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgist.sh
More file actions
39 lines (32 loc) · 717 Bytes
/
gist.sh
File metadata and controls
39 lines (32 loc) · 717 Bytes
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
FNAME="${1:-}"
if [[ ! -f "$FNAME" ]]; then
echo "File not found"
exit 2
else
CONTENT=$(cat "$FNAME")
fi
if [[ ! $gist_token ]]; then
echo 'Token not provided'
exit 2
fi
CONTENT=$(echo "${CONTENT}" | sed -e 's/\\/\\\\/g' -e 's/\r//' -e's/\t/\\t/g' -e 's/"/\\"/g' | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g')
TEMP=$(mktemp)
cat > $TEMP <<EOF
{
"public": false,
"files": {
"$(basename $FNAME)": {
"content": "${CONTENT}"
}
}
}
EOF
OUTPUT=$(curl -H "Authorization: token ${gist_token}" -X POST -d @$TEMP "https://api.github.com/gists" )
URL=$(echo "$OUTPUT" | grep 'html_url' | grep 'gist')
rm $TEMP
if [[ ! -z ${URL:-} ]]; then
echo "URL: $URL"
else
echo "ERROR"
echo "$OUTPUT"
fi