Skip to content

Instantly share code, notes, and snippets.

@danwild
Created September 11, 2020 03:12
Show Gist options
  • Save danwild/1c7c245fe98979aef91c5cda01a590b1 to your computer and use it in GitHub Desktop.
Save danwild/1c7c245fe98979aef91c5cda01a590b1 to your computer and use it in GitHub Desktop.
Gist about making gists (large files from bash)

Taken from: https://stackoverflow.com/a/33354920/1177832

# 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment