我通常将错误报告和日志粘贴到Github的Gist上,以交换编程相关的调试信息。 Gist 没有上传文件的按钮。因此,有时将大型错误报告复制并粘贴到 gists 文本区域中进行输入并不那么方便。
有没有办法将文件从命令行上传到 Gist 帐户中的新 Gist 中?
还为要上传的文件创建一个临时 git 存储库会有所帮助,然后我会在脚本中自动执行此操作。
最后我想用一个 bash 脚本自动在 github 上发布我的编程项目的调试信息
这里是一个适合我在 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"
如果您需要创建与您的 github 帐户关联的要点,(用于基本身份验证)将最后一行替换为:
curl -u "${GITHUB_USERNAME}" -X POST -d "${DESC}" "https://api.github.com/gists"
Python 实现的命令行工具:
pip install python-gist
gist create "example description" foo.txt bar.txt
参见https://github.com/jdowner/gist
如果您更喜欢 Ruby gem,请参阅 https://github.com/defunkt/gist
使用 GitHub CLI:
gh gist create yourfile
或者,为了公共要点,
gh gist create --public yourfile
要添加说明,请使用
--desc
/-d
标志,要直接在 Web 浏览器中打开,请使用 --web
/-w
。
请参阅参考手册了解
gh gist create
。
基于 Sylvain Leroux 的答案,我们可以通过使用 jq 命令行工具来替换清理和 json 构建步骤:
$ jq -Rs '{"description": "some description", "public": true, "files": {"'$FNAME'": {"content": .}}}' $FNAME | curl -X POST -d @- "https://api.github.com/gists"
或者,通过身份验证:
$ jq -Rs '{"description": "some description", "public": true, "files": {"'$FNAME'": {"content": .}}}' $FNAME | curl -u "${GITHUB_USERNAME}" -X POST -d @- "https://api.github.com/gists"
您应该能够创建一个新的 Gist,使用 GitHub API 创建 Gist:
POST /gists
您会发现使用此 API 的各种脚本,例如:
甚至 GitHub 编辑器 Atom.io 也有 gist-it 功能。
Here 是一个执行相同操作的 Python 脚本。它是我积极开发的。自述文件的使用细节非常简单。 一些例子 -
gifc get 5
gifc create create.md -d "How to create a gist from cli" -i nano
gifc create create.md -d "How to create a gist from cli" -m '''If you want to create a gist from an existing file then you do the following- `gifc create create.md -d "How to create a gist from cli" -f file.md`'''
gifc create create.md -d "How to create a gist from cli" -f file.md
迭代编辑所有(或部分)文件
gifc update ffd2f4a482684f56bf33c8726cc6ae63 -i vi
get
方法中获取 gist id
更改描述
gifc update ffd2f4a482684f56bf33c8726cc6ae63 -cd "New description"
get
方法中获取 gist id
在nano、vim或gedit
等编辑器中以交互方式编辑文件内容gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md
gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md -cd "New description"
gifc remove ffd2f4a482684f56bf33c8726cc6ae63 -r file1.md script.py readme.txt
get
方法中获取 gist id
gifc delete ffd2f4a482684f56bf33c8726cc6ae63
get
方法中获取 gist id
对于具有有限 busybox ash shell 的路由器,我创建了这个 shell 脚本帖子和补丁要点。使用方法:
$ pgist my_file_to_post_or_patch_to_gist.extension
无需记住冗长的要点 ID。该 gist 被编码为自动查找相应的 gist ID。限制是:
curl -O https://gist.githubusercontent.com/ProBackup-nl/3971a45b21749cfff6c0069d3dad1dde/raw/pgist.sh && chmod 755 pgist.sh && mv pgist.sh /opt/usr/sbin/pgist
(60) SSL 证书问题:无法获取本地颁发者证书