56 lines
1.3 KiB
Ruby
Raw Normal View History

require "pathname"
require "open3"
2017-08-08 18:10:13 +02:00
def curl_executable
curl = Pathname.new ENV["HOMEBREW_CURL"]
curl = Pathname.new "/usr/bin/curl" unless curl.exist?
2017-08-08 18:10:13 +02:00
return curl if curl.executable?
raise "#{curl} is not executable"
end
2017-08-08 18:10:13 +02:00
def curl_args(*extra_args, show_output: false, user_agent: :default)
args = [
2017-08-08 18:10:13 +02:00
curl_executable.to_s,
"--show-error",
]
2017-08-08 18:10:13 +02:00
args << "--user-agent" << case user_agent
when :browser, :fake
HOMEBREW_USER_AGENT_FAKE_SAFARI
when :default
HOMEBREW_USER_AGENT_CURL
else
2017-08-08 18:10:13 +02:00
user_agent
end
2017-08-08 18:10:13 +02:00
unless show_output
args << "--fail"
args << "--progress-bar" unless ARGV.verbose?
args << "--verbose" if ENV["HOMEBREW_CURL_VERBOSE"]
args << "--silent" if !$stdout.tty? || ENV["TRAVIS"]
end
2017-08-08 18:10:13 +02:00
args + extra_args
end
def curl(*args)
2017-08-08 18:10:13 +02:00
safe_system(*curl_args(*args))
2017-08-04 16:24:29 +02:00
end
2017-08-08 18:10:13 +02:00
def curl_download(*args, to: nil, **options)
continue_at ||= "-"
curl("--location", "--remote-time", "--continue-at", continue_at, "--output", to, *args, **options)
rescue ErrorDuringExecution
# `curl` error 33: HTTP server doesn't seem to support byte ranges. Cannot resume.
if $CHILD_STATUS.exitstatus == 33 && continue_at == "-"
continue_at = "0"
retry
end
raise
2017-08-08 18:10:13 +02:00
end
def curl_output(*args, **options)
Open3.capture3(*curl_args(*args, show_output: true, **options))
end