git 使用 bash 将文件上传到 Gist

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26484337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:29:19  来源:igfitidea点击:

Upload a file to a Gist with bash

gitautomationgist

提问by rubo77

I usually paste error reports and logs on Gistat 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.

我通常会在 Github 的Gist上粘贴错误报告和日志,以交换编程相关的调试信息。Gist 没有上传文件的按钮。因此,有时将大型错误报告复制并粘贴到 gists 文本区域以供输入并不是那么方便。

Is there a way to upload a file from the commandline into a new Gist in your Gist account?

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

also creating a temporary git repository for the file to upload would help, I would automate this in a script then.

还为要上传的文件创建一个临时 git 存储库会有所帮助,然后我会在脚本中自动执行此操作。

In the end I would like to automate posting debug information of my programming project on github with one bash script

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

采纳答案by Sylvain Leroux

Hereis a solution that works for me on Bash/Dash to create anonymousgist (very probably not bullet-proof):

是一个适用于我在 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(
curl -u "${GITHUB_USERNAME}" -X POST -d "${DESC}" "https://api.github.com/gists"
"\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"

If you need to create a gist associated with your github account, (for basic authentication) replace the last line by:

如果您需要创建与您的 github 帐户关联的要点,(用于基本身份验证)将最后一行替换为:

POST /gists

For more advanced authentification schemes, please see https://developer.github.com/v3/#authentication

更高级的认证方案请见https://developer.github.com/v3/#authentication

回答by Andreas

See https://github.com/defunkt/gist

https://github.com/defunkt/gist

It's a Ruby script that can be used from the command line.

这是一个可以从命令行使用的 Ruby 脚本。

回答by Tim Rakowski

Building on the answer of Sylvain Leroux, we can replace the sanitization and json building steps by making use of the jqcommand line tool:

基于 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 -X POST -d @- "https://api.github.com/gists"

Or, with authentication:

或者,使用身份验证:

$ jq -Rs '{"description": "some description", "public": true, "files": {"'$FNAME'": {"content": .}}}' $FNAME | curl -u "${GITHUB_USERNAME}" -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"

回答by VonC

You should be able to create a new Gist, using the GitHub API for creating a Gist:

你应该能够创建一个新的 Gist,使用GitHub API 来创建一个 Gist

##代码##

You will find various script using this API, like:

您会发现使用此 API 的各种脚本,例如:

Even the GitHub editor Atom.iohas a gist-it feature.

甚至 GitHub 编辑器Atom.io也有一个gist-it 功能

https://raw.githubusercontent.com/rpowelll/gist-it/master/media/screencast.gif

https://raw.githubusercontent.com/rpowelll/gist-it/master/media/screencast.gif

回答by jar

Hereis a Python script to do the same. It is actively developed by me. The README is pretty straightforward in its usage details. Some examples-

是一个执行相同操作的 Python 脚本。它是我积极开发的。README 的使用细节非常简单。一些例子-

Get a list of gists

获取要点列表

gifc get 5

gifc get 5

Create a gist

创建一个要点

  • Create interactively from an editor like nano, vimor gedit
    • gifc create create.md -d "How to create a gist from cli" -i nano
  • Directly enter contents from cli
    • 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`'''
  • Take the contents from a file
    • gifc create create.md -d "How to create a gist from cli" -f file.md
  • nanovimgedit等编辑器交互式创建
    • gifc create create.md -d "How to create a gist from cli" -i nano
  • 直接从cli输入内容
    • 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

Update a gist

更新要点

  • Edit all (or some) files iteratively

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -i vi
      You can get the gist idfrom the getmethod from earlier
  • Change description

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -cd "New description"
      You can get the gist idfrom the getmethod from earlier
  • Edit contents of a file interactively in an editor like nano, vimor gedit

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md
  • Do both
    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md -cd "New description"
  • 迭代编辑所有(或部分)文件

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -i vi
      您可以从之前的方法中获取要点 IDget
  • 更改说明

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -cd "New description"
      您可以从之前的方法中获取要点 IDget
  • nanovimgedit等编辑器中以交互方式编辑文件内容

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md
  • 两者都做
    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md -cd "New description"

Delete file(s) from a gist

从 Gist 中删除文件

gifc remove ffd2f4a482684f56bf33c8726cc6ae63 -r file1.md script.py readme.txt
You can get the gist idfrom the getmethod from earlier

gifc remove ffd2f4a482684f56bf33c8726cc6ae63 -r file1.md script.py readme.txt
您可以从之前的方法中获取要点 IDget

Delete the whole gist

删除整个要点

gifc delete ffd2f4a482684f56bf33c8726cc6ae63
You can get the gist idfrom the getmethod from earlier

gifc delete ffd2f4a482684f56bf33c8726cc6ae63
您可以从之前的方法中获取要点 IDget

回答by Pro Backup

For routers with limited busybox ash shells I created this shell script post and patch gister. Usage: $ pgist my_file_to_post_or_patch_to_gist.extension

对于busybox ash shell 有限的路由器,我创建了这个shell 脚本帖子和补丁gister。用法:$ pgist my_file_to_post_or_patch_to_gist.extension

No need to remember long gist ID's. This gister is coded to automagically lookup the corresponding gist ID. Limitations are:

无需记住长的要点 ID。这个 gister 被编码为自动查找相应的 gist ID。限制是:

  1. up to 30 (maybe 100) gists in your account
  2. up to 300 files per gist
  3. unique filenames for all your 30×300 gist files
  4. it's not able to post/patch itself due to "Problems parsing JSON"
  1. 您的帐户中最多 30 个(也许 100 个)要点
  2. 每个要点最多 300 个文件
  3. 所有 30×300 gist 文件的唯一文件名
  4. 由于“解析 JSON 问题”,它无法发布/修补自身

Installation example

安装示例

curl -O https://gist.githubusercontent.com/ProBackup-nl/3971a45b21749cfff6c0069d3dad1dde/raw/pgist.sh && chmod 755 pgist.sh && mv pgist.sh /opt/usr/sbin/pgist

curl -O https://gist.githubusercontent.com/ProBackup-nl/3971a45b21749cfff6c0069d3dad1dde/raw/pgist.sh && chmod 755 pgist.sh && mv pgist.sh /opt/usr/sbin/pgist

Dependencies

依赖关系

  • github oauth token
  • jq
  • sed
  • awk
  • curland ca-certificatesto create a valid certificate chain instead of

(60) SSL certificate problem: unable to get local issuer certificate

(60) SSL证书问题:无法获取本地颁发者证书