rub*_*o77 19 git automation gist
我通常粘贴错误报告和日志要点在Github上,交换编程相关的调试信息.Gist没有上传文件的按钮.因此,有时将大型错误报告复制并粘贴到gists textarea中以进行输入并不方便.
有没有办法将命令行中的文件上传到Gist帐户中的新Gist?
还要为要上传的文件创建临时git存储库会有所帮助,我会在脚本中自动执行此操作.
最后,我想使用一个bash脚本自动在github上发布我的编程项目的调试信息
Syl*_*oux 10
这是一个解决方案,适用于我在Bash/Dash上创建匿名要点(很可能不是防弹):
# 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"
Run Code Online (Sandbox Code Playgroud)
如果您需要创建与您的github帐户关联的要点,(对于基本身份验证)将最后一行替换为:
curl -u "${GITHUB_USERNAME}" -X POST -d "${DESC}" "https://api.github.com/gists"
Run Code Online (Sandbox Code Playgroud)
有关更高级的身份验证方案,请参阅https://developer.github.com/v3/#authentication
使用GitHub CLI:
gh gist create yourfile
Run Code Online (Sandbox Code Playgroud)
或者,为了公共要点,
gh gist create --public yourfile
Run Code Online (Sandbox Code Playgroud)
要添加描述,请使用--desc
/-d
标志,要直接在 Web 浏览器中打开,请使用--web
/ -w
。
基于 Sylvain Leroux 的答案,我们可以通过使用jq命令行工具来替换清理和 json 构建步骤:
$ jq -Rs '{"description": "some description", "public": true, "files": {"'$FNAME'": {"content": .}}}' $FNAME | curl -X POST -d @- "https://api.github.com/gists"
Run Code Online (Sandbox Code Playgroud)
或者,通过身份验证:
$ jq -Rs '{"description": "some description", "public": true, "files": {"'$FNAME'": {"content": .}}}' $FNAME | curl -u "${GITHUB_USERNAME}" -X POST -d @- "https://api.github.com/gists"
Run Code Online (Sandbox Code Playgroud)