How pass cookies to custom strategy based on CurlDownloadStrategy

Basically, the following formula works for gitlab to use a private tap:

# 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

Running brew install mycompany/mytool --debug show me the options -b is added:

curl --disable --globoff --show-error --retry 3 -b _gitlab_session=**** --location --head --request GET ...

Now the question: how can I wrap this into a PrivateRepositoryDownloadStrategy as follows (which does not work properly):

require "download_strategy"

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

  private

  def _fetch(url:, resolved_url:)
    args = ["-b", "_gitlab_session", "#{@gitlab_session_token}"]
    curl_download(@url, *args, to: temporary_path)
  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

and adapt the formula from above with a require_relative "../lib/PrivateRepositoryDownloadStrategy" where I store the strategy.

When I add the tap and try to install it via brew install mycompany/mytool --debug the -b options is not considered but only the default options :

curl --disable --globoff --show-error --retry 3 --location --head --request GET ...

How can I add the option to the CurlDownloadStrategy?

Best Manu

I solved it. Maybe it helps someone out there :slight_smile:

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
1 Like