Skip to content

Instantly share code, notes, and snippets.

@mikewlange
Forked from rubo77/create-gist.sh
Created March 31, 2019 21:56
Show Gist options
  • Save mikewlange/b187fcfbcd684d4d516921221ee4685f to your computer and use it in GitHub Desktop.
Save mikewlange/b187fcfbcd684d4d516921221ee4685f to your computer and use it in GitHub Desktop.
Post GIST
#!/bin/bash
GITHUB_USERNAME=rubo77
if [ "$1" == "" ]; then
echo 'usage: gistfile-post.sh filename [gistname]'
exit 0
fi
# 0. file name for the Gist
if [ "$2" == "" ]; then
FNAME="$1"
elif
FNAME="$2"
fi
# 1. Somehow sanitize the file content
# Remove \r (from Windows end-of-lines),
# Replace tabs by \t
# Replace " by \"
# Replace EOL by \n
CONTENT=$(sed -e 's/\r//' -e's/\t/\\t/g' -e 's/"/\\"/g' "${FNAME}" | awk '{ printf($0 "\\n") }')
# 2. Build the JSON request
read -r -d '' DESC <<EOF
{
"description": "some description",
"public": true,
"files": {
"${FNAME}": {
"content": "${CONTENT}"
}
}
}
EOF
# 3. Use curl to send a POST request
# ANONYMOUS GIST :
# curl -X POST -d "${DESC}" "https://api.github.com/gists"
# REGISTERED USER
curl -u "${GITHUB_USERNAME}" -X POST -d "${DESC}" "https://api.github.com/gists"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment