Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload a file to a Gist with bash

I usually paste error reports and logs on Gist at Github, to exchange programming relevant debug information. Gist doesn't have a button to upload a file. So sometimes it is not so convenient to copy and paste your large errorreports into gists textarea for input.

Is there a way to upload a file from the commandline into a new Gist in your Gist account?

also creating a temporary git repository for the file to upload would help, I would automate this in a script then.

In the end I would like to automate posting debug information of my programming project on github with one bash script

like image 832
rubo77 Avatar asked Oct 21 '14 10:10

rubo77


People also ask

How do I paste code into gist?

copy the embedding JavaScript code from GitHub and directly paste it in the body of the [gist] tag: [gist]<script src="https://gist.github.com/447298.js?file=github_gist_wordpress_plugin_test.txt"></script>[/gist]. (at your option) any later version.

Can you edit a gist?

How Do I Edit or Delete a Gist? In the top right corner of your gist page, there will be a menu that allows for multiple functions to be performed on your gist. We can edit, delete, unsubscribe, star, embed, copy, share, and download a raw copy or zipped copy of a gist.


1 Answers

Here is a solution that works for me on Bash/Dash to create anonymous gist (very probably not bullet-proof):

# 0. Your file name FNAME=some.file  # 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 curl -X POST -d "${DESC}" "https://api.github.com/gists" 

If you need to create a gist associated with your github account, (for basic authentication) replace the last line by:

curl -u "${GITHUB_USERNAME}" -X POST -d "${DESC}" "https://api.github.com/gists" 

For more advanced authentification schemes, please see https://developer.github.com/v3/#authentication

like image 135
Sylvain Leroux Avatar answered Sep 22 '22 12:09

Sylvain Leroux