git - 使用 bash 将文件上传到 Gist

标签 git automation github-api gist

我通常会在Gist 上粘贴错误报告和日志在GitHub,交换相关调试信息。 Gist 没有一个按钮可以上传文件。因此,有时将大型错误报告复制并粘贴到Gists Textarea中并不是那么方便。

有没有办法将文件从命令行上传到您的 Gist 帐户中的新 Gist ?

还创建一个临时的git存储库来上传文件,我将在脚本中自动化。

最后,我想通过一个bash脚本在github上自动发布我的编程项目的调试信息

最佳答案

Here是一个解决方案,适用于我在 Bash/Dash 上创建匿名 Gist (很可能不是防弹的):

# 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 帐户关联的 Gist ,(用于基本身份验证)将最后一行替换为:

curl -u "${GITHUB_USERNAME}" -X POST -d "${DESC}" "https://api.github.com/gists"

有关更高级的身份验证方案,请参阅 https://developer.github.com/v3/#authentication

关于git - 使用 bash 将文件上传到 Gist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26484337/

相关文章:

perl - Perl 5 中有哪些工具可以使外部程序自动化?

android - Android应用程序的自动化测试工具

javascript - 使用 Octokit 或 GitHub Rest API 上传多个文件

git - 是否有命令行工具可以实时显示 git 状态?

git push -u origin master 失败,遇到权限被拒绝的问题

git - 无法从 Gitlab : Permission denied, 克隆存储库,请重试

android - 我们如何将 Genymotion 与 Eclipse 和 Appium 一起使用

ruby - HTTParty POST 正在发送 GET

github - 使用 Github api 获取 react

windows - 权限被拒绝尝试设置全局git配置