Merge pull request #14221 from MikeMcQuaid/api_formula_handle_corruption

api/formula: handle JSON file corruption.
This commit is contained in:
Mike McQuaid 2022-12-08 10:06:50 +00:00 committed by GitHub
commit 69ef011985
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,8 @@ module Homebrew
class << self
extend T::Sig
MAX_RETRIES = 3
sig { returns(String) }
def formula_api_path
"formula"
@ -29,13 +31,24 @@ module Homebrew
sig { returns(Hash) }
def all_formulae
@all_formulae ||= begin
curl_args = %w[--compressed --silent https://formulae.brew.sh/api/formula.json]
retry_count = 0
url = "https://formulae.brew.sh/api/formula.json"
json_formulae = begin
curl_args = %W[--compressed --silent #{url}]
if cached_formula_json_file.exist? && !cached_formula_json_file.empty?
curl_args.prepend("--time-cond", cached_formula_json_file)
end
curl_download(*curl_args, to: cached_formula_json_file, max_time: 5)
json_formulae = JSON.parse(cached_formula_json_file.read)
JSON.parse(cached_formula_json_file.read)
rescue JSON::ParserError
cached_formula_json_file.unlink
retry_count += 1
odie "Cannot download non-corrupt #{url}!" if retry_count > MAX_RETRIES
retry
end
@all_aliases = {}
json_formulae.to_h do |json_formula|