Homebrew 使用 curl 选项扩展 CurlDownloadStrategy

Posted

技术标签:

【中文标题】Homebrew 使用 curl 选项扩展 CurlDownloadStrategy【英文标题】:Homebrew extend CurlDownloadStrategy with curl option 【发布时间】:2020-09-02 13:55:10 【问题描述】:

有谁知道如何使用标题扩展自制软件中的CurlDownloadStrategy 吗?我已经在这里和 github 问题中进行了研究,但找不到任何简单且有效的答案。基本上它是给 gitlab 的,我需要设置一个标题。

我在这里找到了一个snippet,修正了一个错字,但是当我运行brew install mytab/mytool --debug 它不使用给定的--header 选项,而是使用官方code 中描述的标准选项

这是我所拥有的:

require "download_strategy"

class PrivateRepositoryDownloadStrategy < CurlDownloadStrategy
  def initialize(url, name, version, **meta)
    super
    set_gitlab_token
  end

  private

  def _fetch(url:, resolved_url:)
    args = ["--header", "Private-Token: #@gitlab_token"]
    curl_download(@url, *args, to: temporary_path)
  end

  def set_gitlab_token
    @gitlab_token = ENV["HOMEBREW_GITLAB_ACCESS_TKN"]
    unless @gitlab_token
      raise CurlDownloadStrategyError, "Environment variable HOMEBREW_GITLAB_ACCESS_TKN is required."
    end
  end
end

有什么想法吗? :)

【问题讨论】:

【参考方案1】:

好的,我解决了这个问题。基本上我需要能够从存储库的 gitlab 上传下载工件。问题是我们需要将自定义标头传递给curl,而CurlDownloadStrategy 已经可以根据snippet 传递自定义标头和cookie (-b)。我在路上学到了一些红宝石,不再需要PrivateRepositoryDownloadStrategy :)

所以我的最终公式如下所示:

# This file was generated by GoReleaser. DO NOT EDIT.
class MyTool < Formula
  desc "mytool desc"
  homepage "https://gitab.mycompany/group/tool"
  version "0.0.1"
  bottle :unneeded

  if OS.mac?
    url "https://gitab.mycompany/group/tool/uploads/d046fcf878e88dd02312f15da23c5e00/mytool_Darwin_x86_64.tar.gz", :cookies => [["_gitlab_session", "#ENV['HOMEBREW_GITLAB_SESSION_ACCESS_TOKEN']"]]
    sha256 "8f3957fdf78fde15d900229b29cae81c490eb585ff220acd7f0d71b4244f8d02"
  elsif OS.linux?
    if Hardware::CPU.intel?
      url "https://gitab.mycompany/group/tool/uploads/6082ce9fead78d6029c9ac091d4dacda/mytool_Linux_x86_64.tar.gz", :cookies => [["_gitlab_session", "#ENV['HOMEBREW_GITLAB_SESSION_ACCESS_TOKEN']"]]
      sha256 "d9713c89f565f2981ef7bc7a63d87ab7f7d84a00c7b3ffbc585ff959097f3d64"
    end
  end

  def install
    bin.install "mytool"
  end

  test do
    system "#bin/mytool --help"
  end
end

更新: 我还设法修复了自定义公式

require "download_strategy"

class PrivateRepositoryDownloadStrategy < CurlDownloadStrategy
  def initialize(url, name, version, **meta)
    super
    set_gitlab_session_token
  end

  private

  def _curl_args
    args = ["-b", "_gitlab_session=#@gitlab_session_token"]
    args
  end

  def set_gitlab_session_token
    @gitlab_session_token = ENV["HOMEBREW_GITLAB_SESSION_ACCESS_TOKEN"]
    unless @gitlab_session_token
      raise CurlDownloadStrategyError, "Environment variable HOMEBREW_GITLAB_SESSION_ACCESS_TOKEN is required."
    end
  end
end

和一个适应的公式

require_relative "./PrivateRepositoryDownloadStrategy"
class MyTool < Formula
  desc "mytool desc"
  homepage "https://gitab.mycompany/group/tool"
  version "0.0.1"
  bottle :unneeded

  if OS.mac?
    url "https://gitab.mycompany/group/tool/uploads/d046fcf878e88dd02312f15da23c5e00/mytool_Darwin_x86_64.tar.gz", :using => PrivateRepositoryDownloadStrategy
    sha256 "8f3957fdf78fde15d900229b29cae81c490eb585ff220acd7f0d71b4244f8d02"
  elsif OS.linux?
    if Hardware::CPU.intel?
      url "https://gitab.mycompany/group/tool/uploads/6082ce9fead78d6029c9ac091d4dacda/mytool_Linux_x86_64.tar.gz", :using => PrivateRepositoryDownloadStrategy
      sha256 "d9713c89f565f2981ef7bc7a63d87ab7f7d84a00c7b3ffbc585ff959097f3d64"
    end
  end

  def install
    bin.install "mytool"
  end

  test do
    system "#bin/mytool --help"
  end
end

【讨论】:

以上是关于Homebrew 使用 curl 选项扩展 CurlDownloadStrategy的主要内容,如果未能解决你的问题,请参考以下文章

curl几个选项

php5.6.27怎么加载curl

Homebrew 的使用

sh 将装有curl的Homebrew连接到你的Github前叉

Mac 下安装 HomeBrew;RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54

HomeBrew的安装与卸载