Skip to content

Instantly share code, notes, and snippets.

@matthax
Created March 21, 2018 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthax/73e19956194c710ce5ad85fcc0b7f88e to your computer and use it in GitHub Desktop.
Save matthax/73e19956194c710ce5ad85fcc0b7f88e to your computer and use it in GitHub Desktop.
uploads a file as a gist
#!/usr/bin/bash
# https://stackoverflow.com/questions/26484337/upload-a-file-to-a-gist-with-bash
# https://gist.github.com/rubo77/3b8b15f4df5b5287a24f87523c41e4ce
# TODO: read this from a config
GITHUB_USERNAME=matthax
if [ "$1" == "" ]; then
echo 'usage: gist filename [gistname] [description]'
exit 0
fi
# 0. file name for the Gist
if [ "$2" == "" ]; then
FNAME="$1"
else
FNAME="$2"
fi
if [ "$3" == "" ]; then
DESCRIPTION="${FNAME} uploaded via bash"
else
DESCRIPTION=$3
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": "${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