Skip to content

Instantly share code, notes, and snippets.

@MonteLogic
Forked from ryanli1994/upload_to_gist.sh
Created September 3, 2022 05:53
Show Gist options
  • Save MonteLogic/3bd719da11c8f18a2825a6e7d7094a7c to your computer and use it in GitHub Desktop.
Save MonteLogic/3bd719da11c8f18a2825a6e7d7094a7c to your computer and use it in GitHub Desktop.
upload file to gist bash
# 0. Your file name
FNAME=worker.log
GITHUB_USERNAME=kigawas
# 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 -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