Skip to content

Instantly share code, notes, and snippets.

@rubo77

rubo77/create-gist.sh

Forked from s-leroux/pgist.sh
Last active Aug 30, 2020
Embed
What would you like to do?
A utility to create a gists from command line - for support, please check the repo https://github.com/ceremcem/create-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"
else
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"
@ceremcem

This comment has been minimized.

Copy link

@ceremcem ceremcem commented Sep 12, 2017

This code deserves a repo: https://github.com/ceremcem/create-gist

@dtasev

This comment has been minimized.

Copy link

@dtasev dtasev commented Nov 24, 2017

Is elif on line 13 an error? My bash complains but it works when changed to else

@chankruze

This comment has been minimized.

Copy link

@chankruze chankruze commented Mar 12, 2019

Is elif on line 13 an error? My bash complains but it works when changed to else

Yes, That should be else.

@hsandt

This comment has been minimized.

Copy link

@hsandt hsandt commented Aug 24, 2019

I needed to edit an existing gist to upload a binary file, which cannot be done via the UI.

To edit a gist, add a parameter $GIST_ID, replace POST with PATCH in curl, and after /gists add /$GIST_ID.
I still had formatting issues so I couldn't test it to the end, though. But I think that can work for normal text editing.

Then I tried the gist command line but defunkt/gist#191 mentions binaries cannot be uploaded.

So I followed the workaround at the end, cloning the gist repo and adding them manually locally, then pushing them.

@rubo77

This comment has been minimized.

Copy link
Owner Author

@rubo77 rubo77 commented Aug 24, 2019

Is elif on line 13 an error? My bash complains but it works when changed to else

Yes, That should be else.

thx, I corrected that

@rubo77

This comment has been minimized.

Copy link
Owner Author

@rubo77 rubo77 commented Aug 24, 2019

This was original asked here: https://stackoverflow.com/questions/26484337/upload-a-file-to-a-gist-with-bash and we continued the development from there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment