121 lines
4.0 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
require "utils/github/actions"
2018-09-06 08:29:14 +02:00
module Cask
2018-09-04 08:45:48 +01:00
class Cmd
2021-01-25 09:18:10 +00:00
# Cask implementation of the `brew audit` command.
2020-08-19 10:34:07 +02:00
#
# @api private
2017-05-20 19:08:03 +02:00
class Audit < AbstractCommand
2020-10-20 12:03:48 +02:00
extend T::Sig
2020-08-01 02:30:46 +02:00
def self.parser
super do
2021-03-18 14:46:48 +00:00
switch "--[no-]download",
2020-08-01 02:30:46 +02:00
description: "Audit the downloaded file"
switch "--[no-]appcast",
2020-08-01 02:30:46 +02:00
description: "Audit the appcast"
2021-03-18 14:46:48 +00:00
switch "--[no-]token-conflicts",
2020-08-01 02:30:46 +02:00
description: "Audit for token conflicts"
2022-08-01 14:30:04 +02:00
switch "--[no-]signing",
description: "Audit for signed apps, which is required on ARM"
2021-03-18 14:46:48 +00:00
switch "--[no-]strict",
2020-08-01 02:30:46 +02:00
description: "Run additional, stricter style checks"
2021-03-18 14:46:48 +00:00
switch "--[no-]online",
2020-08-01 02:30:46 +02:00
description: "Run additional, slower style checks that require a network connection"
switch "--new-cask",
2020-08-18 16:14:30 +01:00
description: "Run various additional style checks to determine if a new cask is eligible " \
"for Homebrew. This should be used when creating new casks and implies " \
"`--strict` and `--online`"
2021-03-21 13:59:43 -04:00
switch "--display-failures-only",
description: "Only display casks that fail the audit. This is the default for formulae."
2020-08-01 02:30:46 +02:00
end
2016-09-24 13:52:43 +02:00
end
2020-10-20 12:03:48 +02:00
sig { void }
2016-09-24 13:52:43 +02:00
def run
casks = args.named.flat_map do |name|
2020-11-13 17:21:51 +01:00
next name if File.exist?(name)
next Tap.fetch(name).cask_files if name.count("/") == 1
name
2020-09-29 23:46:30 +02:00
end
casks = casks.map { |c| CaskLoader.load(c, config: Config.from_args(args)) }
any_named_args = casks.any?
casks = Cask.to_a if casks.empty?
2020-11-18 12:41:18 +01:00
results = self.class.audit_casks(
*casks,
2021-03-21 13:59:43 -04:00
download: args.download?,
appcast: args.appcast?,
online: args.online?,
strict: args.strict?,
2022-08-01 14:30:04 +02:00
signing: args.signing?,
2021-03-21 13:59:43 -04:00
new_cask: args.new_cask?,
token_conflicts: args.token_conflicts?,
quarantine: args.quarantine?,
any_named_args: any_named_args,
language: args.language,
display_passes: args.verbose? || args.named.count == 1,
display_failures_only: args.display_failures_only?,
only: [],
except: [],
)
2020-11-18 12:41:18 +01:00
failed_casks = results.reject { |_, result| result[:errors].empty? }.map(&:first)
return if failed_casks.empty?
raise CaskError, "audit failed for casks: #{failed_casks.join(" ")}"
end
def self.audit_casks(
*casks,
download:,
appcast:,
online:,
strict:,
signing:,
new_cask:,
token_conflicts:,
quarantine:,
any_named_args:,
language:,
display_passes:,
display_failures_only:,
only:,
except:
)
options = {
audit_download: download,
audit_appcast: appcast,
audit_online: online,
audit_strict: strict,
2022-08-01 14:30:04 +02:00
audit_signing: signing,
audit_new_cask: new_cask,
audit_token_conflicts: token_conflicts,
quarantine: quarantine,
language: language,
any_named_args: any_named_args,
2021-03-21 13:59:43 -04:00
display_passes: display_passes,
display_failures_only: display_failures_only,
only: only,
except: except,
}.compact
options[:quarantine] = true if options[:quarantine].nil?
Homebrew.auditing = true
require "cask/auditor"
2021-12-23 14:49:05 -05:00
casks.to_h do |cask|
2020-04-23 21:16:17 +02:00
odebug "Auditing Cask #{cask}"
2021-04-03 03:49:41 +02:00
[cask.sourcefile_path, Auditor.audit(cask, **options)]
2021-12-23 14:49:05 -05:00
end
2020-11-18 12:41:18 +01:00
end
2016-08-18 22:11:42 +03:00
end
end
end