问题描述
我通常粘贴错误报告,并在Github上登录 Gist ,以交换与编程相关的调试信息.要点没有按钮来上传文件.因此有时将大的错误报告复制并粘贴到要点文本区域中以进行输入不是很方便.
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.
是否可以通过命令行将文件上传到Gist帐户中的新Gist中?
Is there a way to upload a file from the commandline into a new Gist in your Gist account?
还为要上传的文件创建一个临时git存储库会有所帮助,然后我将在脚本中将其自动化.
also creating a temporary git repository for the file to upload would help, I would automate this in a script then.
最后,我想使用一个bash脚本自动将我的编程项目的调试信息发布到github上
In the end I would like to automate posting debug information of my programming project on github with one bash script
推荐答案
此处是一种在Bash/Dash上对我有用的解决方案,用于创建匿名要点(很可能不是防弹的):
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"
如果您需要创建与github帐户关联的要点,(用于基本身份验证)将最后一行替换为:
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"
有关更高级的身份验证方案,请参见 https://developer.github.com/v3/#身份验证
For more advanced authentification schemes, please see https://developer.github.com/v3/#authentication
这篇关于用bash将文件上传到Gist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!