【问题标题】:How to create a gist on command line如何在命令行上创建要点
【发布时间】:2015-12-02 16:44:28
【问题描述】:

我正在尝试从 bash 创建一个要点,并且我已经尝试了许多我可以获得的版本的脚本,但都没有工作。

这似乎是正确的,但它也不起作用。

curl -X POST -d '{"public":true,"files":{"test.txt":{"content":"String file contents"}}}' https://api.github.com/gists

我有一个test.txt 文件,其中包含我想创建要点的内容,但它不起作用。它说,invalid email,如果我尝试添加-u USER-u USER:PASS,它仍然无法说"message": "Problems parsing JSON", ..

我不知道出了什么问题。 documentation 除了这一行之外没有提供太多:

POST /gists 如您所见,我正在传递 test.txt 文件。

【问题讨论】:

    标签: github github-api gist


    【解决方案1】:

    最近发布了 GitHub CLI。 所以你现在可以改用它了。

    只需将其安装到您的系统 (https://github.com/cli/cli#installation)

    身份验证(很简单)

    gh auth login
    

    登录后,您可以通过以下方式简单地创建一个新要点:

    gh gist create -d "my test gist" -f some_local_file.txt  test_gist
    

    更多细节可以使用帮助:

    gh <command> <subcommand> --help
    

    【讨论】:

    • 要在NixOSNix 包管理器上使用它,请使用nix-shell -p github-cli
    • @Nickolay Kondratenko 我正在尝试将新的本地文件推送到 gist 中,但它给了我一个错误 failed to collect files for posting: failed to read file test_gist: open test_gist: no such file or directory gh gist create -d "Snwflake-Queries" -f Snflk_Query.sql test_gist
    • @Karthik 那是因为 test_gist 文件不存在。您的命令应该是以下gh gist create -d "Snwflake-Queries" Snflk_Query.sql
    • @Karthik 来自帮助:-f, --filename string Provide a filename to be used when reading from STDIN
    • @NickolayKondratenko 非常感谢。用新更改覆盖同一文件的命令是什么
    【解决方案2】:

    我刚试过

    curl -X POST -d '{"public":true,"files":{"test.txt":{"content":"String file contents"}}}' -u mgarciaisaia:mypassword https://api.github.com/gists
    

    它成功了:https://gist.github.com/mgarciaisaia/fa51238073eb2cf508aa

    我没有发现你的命令有任何问题。

    【讨论】:

    • btw:我一直在尝试用变量替换 test.txtString file.. ,但是在单引号内,它不起作用。您知道如何在单引号内传递变量吗?
    • 但是,我们如何使用它来获取 file.cpp sn-p 呢?
    • 现在显示 "message": "Not Found", "documentation_url": "developer.github.com/v3"
    • stackoverflow.com/a/64025613/9130688 响应有效期为 2020 年
    • @DimitriKopriwa 您需要将文件的内容读入一个变量,并将其包含在有效负载中。这也意味着更改引号和转义。也许现在有一个新的端点?
    【解决方案3】:

    这个问题很老,所以我不确定它是否仍然相关。

    在 Ubuntu(至少在 18.04)上,您可以尝试使用 gist 软件包,它将安装您可以使用的 gist-paste 命令(假设您已经有 git 帐户),如下所示:

    1) 获取一个 gist OAuth2 令牌(它将使用该令牌创建一个 ~/.gist 文件)。您只需执行一次:

    $ gist-paste --login
    

    然后,您可以发送文件,例如:

    $ gist-paste your-file.txt
    $ cat .emacs.d/init.el | gist-paste -t el
    

    有很多选项:您可以发送文件类型/描述(如上第二个示例)、删除 gist、在浏览器中打开 gist 等...参见gist-paste(1) 或尝试gist-paste --help

    如果您已经拥有 gist 令牌,则无需运行 gist-paste --login,只需将您的 ~/.gitconfigoauth-token 复制到 ~/.gist 即可。 例如,如果您在~/.gitconfig:

    [github]
        oauth-token = foobar123
    

    只需创建一个~/.gist 文件,其中一行包含“foobar123”。

    [编辑] 如果您的发行版不提供包,则项目页面为: https://github.com/defunkt/gist

    【讨论】:

    • 确认可以在 Ubuntu 16.04 上工作,但我只是做了 gist finename.txt
    【解决方案4】:

    我刚刚开始使用http://defunkt.io/gist,它让事情变得非常简单:

    # upload string to file
    gist -f test.txt <<< "hello string"
    
    # upload a file
    gist test.txt
    

    【讨论】:

    • 这太棒了!要安装它,请使用sudo gem install gist,然后使用gist --login,并将8位代码放入github.com。要编写多行代码,只需键入 gist 并在几行代码后使用 CTRL-D 退出。
    • 或者,如果有人在NixOS 上或使用Nix 作为包管理器,请使用nix-shell -p gist
    【解决方案5】:

    有同样的愿望,我找到了https://www.npmjs.com/package/gistup 并将存储库 fork 到https://github.com/CrandellWS/mkg,因为开发人员不想支持当时使用的操作系统 Windows。所以我重新设计了 npm 包以在 windows 以及 linux 和苹果上工作......

    GitHub 上提供了完整源代码: https://github.com/CrandellWS/mkg

    使用 npm 安装很简单

    npm install -g mkg
    

    使用在 npmjs 包页面上有描述: https://www.npmjs.com/package/gistup

    安装后只需cd,您想从中创建要点的每个目录...(请记住,没有包含要点的子文件夹)

    并运行命令:

    mkg
    

    它会在浏览器中打开您的新 gist...此外,您将能够像普通 git 一样从那里控制它...只是没有子文件夹...

    【讨论】:

      【解决方案6】:

      截至September 2020,创建要点是GitHub's official command line tool gh 的功能之一。

      例如,在 MacOS 上:

      brew install gh
      gh auth login  # Follow steps.
      gh gist create myfile.txt  # Creates a private gist.
      

      提供更多选项。 gh gist create --help 给:

      Create a new GitHub gist with given contents.
      
      Gists can be created from one or multiple files. Alternatively, pass "-" as
      file name to read from standard input.
      
      By default, gists are private; use '--public' to make publicly listed ones.
      
      
      USAGE
        gh gist create [<filename>... | -] [flags]
      
      FLAGS
        -d, --desc string       A description for this gist
        -f, --filename string   Provide a filename to be used when reading from STDIN
        -p, --public            List the gist publicly (default: private)
      
      INHERITED FLAGS
        --help   Show help for command
      
      EXAMPLES
        # publish file 'hello.py' as a public gist
        $ gh gist create --public hello.py
      
        # create a gist with a description
        $ gh gist create hello.py -d "my Hello-World program in Python"
      
        # create a gist containing several files
        $ gh gist create hello.py world.py cool.txt
      
        # read from standard input to create a gist
        $ gh gist create -
      
        # create a gist from output piped from another command
        $ cat cool.txt | gh gist create
      
      LEARN MORE
        Use 'gh <command> <subcommand> --help' for more information about a command.
        Read the manual at https://cli.github.com/manual
      

      【讨论】:

      • 如果我想更新 gist 中的现有文件,使用 gh 的命令是什么
      • 使用gh gist edit {&lt;gist ID&gt; | &lt;gist URL&gt;} -f &lt;filename&gt;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 2019-03-21
      • 2015-07-12
      • 2013-09-16
      相关资源
      最近更新 更多