Upload a file to a Gist with bash

2020-05-19 06:14发布

问题:

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

回答1:

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



回答2:

See https://github.com/defunkt/gist

It's a Ruby script that can be used from the command line.



回答3:

You should be able to create a new Gist, using the GitHub API for creating a Gist:

POST /gists

You will find various script using this API, like:

  • this gist.el emacs function.
  • this php script
  • this curl command

Even the GitHub editor Atom.io has a gist-it feature.



回答4:

Building on the answer of Sylvain Leroux, we can replace the sanitization and json building steps by making use of the jq command line tool:

$ jq -Rs '{"description": "some description", "public": true, "files": {"'$FNAME'": {"content": .}}}' $FNAME | curl -X POST -d @- "https://api.github.com/gists"

Or, with authentication:

$ jq -Rs '{"description": "some description", "public": true, "files": {"'$FNAME'": {"content": .}}}' $FNAME | curl -u "${GITHUB_USERNAME}" -X POST -d @- "https://api.github.com/gists"



回答5:

Here is a Python script to do the same. It is actively developed by me. The README is pretty straightforward in its usage details. Some examples-

Get a list of gists

gifc get 5

Create a gist

  • Create interactively from an editor like nano, vim or gedit
    • gifc create create.md -d "How to create a gist from cli" -i nano
  • Directly enter contents from cli
    • gifc create create.md -d "How to create a gist from cli" -m '''If you want to create a gist from an existing file then you do the following- `gifc create create.md -d "How to create a gist from cli" -f file.md`'''
  • Take the contents from a file
    • gifc create create.md -d "How to create a gist from cli" -f file.md

Update a gist

  • Edit all (or some) files iteratively

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -i vi
      You can get the gist id from the get method from earlier
  • Change description

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -cd "New description"
      You can get the gist id from the get method from earlier
  • Edit contents of a file interactively in an editor like nano, vim or gedit

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md
  • Do both
    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md -cd "New description"

Delete file(s) from a gist

gifc remove ffd2f4a482684f56bf33c8726cc6ae63 -r file1.md script.py readme.txt
You can get the gist id from the get method from earlier

Delete the whole gist

gifc delete ffd2f4a482684f56bf33c8726cc6ae63
You can get the gist id from the get method from earlier



回答6:

For routers with limited busybox ash shells I created this shell script post and patch gister. Usage: $ pgist my_file_to_post_or_patch_to_gist.extension

No need to remember long gist ID's. This gister is coded to automagically lookup the corresponding gist ID. Limitations are:

  1. up to 30 (maybe 100) gists in your account
  2. up to 300 files per gist
  3. unique filenames for all your 30×300 gist files
  4. it's not able to post/patch itself due to "Problems parsing JSON"

Installation example

curl -O https://gist.githubusercontent.com/ProBackup-nl/3971a45b21749cfff6c0069d3dad1dde/raw/pgist.sh && chmod 755 pgist.sh && mv pgist.sh /opt/usr/sbin/pgist

Dependencies

  • github oauth token
  • jq
  • sed
  • awk
  • curl and ca-certificates to create a valid certificate chain instead of

(60) SSL certificate problem: unable to get local issuer certificate