diff options
| author | Markus Reiter | 2016-09-24 13:52:43 +0200 |
|---|---|---|
| committer | Markus Reiter | 2016-09-24 16:00:58 +0200 |
| commit | b86c8efb79b3ed835d552c4d7416640ef10caf21 (patch) | |
| tree | 7e1edc8a8f339e4d2781f43576d40c9c79aebcdc /Library/Homebrew/cask/lib/hbc/verify | |
| parent | 687f0fcf721c8e36f32570ed72d0988a6eaf986f (diff) | |
| download | brew-b86c8efb79b3ed835d552c4d7416640ef10caf21.tar.bz2 | |
Cask: Use nested classes and modules.
Diffstat (limited to 'Library/Homebrew/cask/lib/hbc/verify')
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/verify/checksum.rb | 80 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/verify/gpg.rb | 92 |
2 files changed, 90 insertions, 82 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/verify/checksum.rb b/Library/Homebrew/cask/lib/hbc/verify/checksum.rb index 3af6f1667..d079a4446 100644 --- a/Library/Homebrew/cask/lib/hbc/verify/checksum.rb +++ b/Library/Homebrew/cask/lib/hbc/verify/checksum.rb @@ -1,43 +1,47 @@ require "digest" -class Hbc::Verify::Checksum - def self.me?(cask) - return true unless cask.sha256 == :no_check - ohai "No checksum defined for Cask #{cask}, skipping verification" - false - end - - attr_reader :cask, :downloaded_path - - def initialize(cask, downloaded_path) - @cask = cask - @downloaded_path = downloaded_path - end - - def verify - return unless self.class.me?(cask) - ohai "Verifying checksum for Cask #{cask}" - verify_checksum - end - - private - - def expected - @expected ||= cask.sha256 - end - - def computed - @computed ||= Digest::SHA2.file(downloaded_path).hexdigest - end - - def verify_checksum - raise Hbc::CaskSha256MissingError, "sha256 required: sha256 '#{computed}'" if expected.nil? || expected.empty? - - if expected == computed - odebug "SHA256 checksums match" - else - ohai 'Note: running "brew update" may fix sha256 checksum errors' - raise Hbc::CaskSha256MismatchError.new(downloaded_path, expected, computed) +module Hbc + module Verify + class Checksum + def self.me?(cask) + return true unless cask.sha256 == :no_check + ohai "No checksum defined for Cask #{cask}, skipping verification" + false + end + + attr_reader :cask, :downloaded_path + + def initialize(cask, downloaded_path) + @cask = cask + @downloaded_path = downloaded_path + end + + def verify + return unless self.class.me?(cask) + ohai "Verifying checksum for Cask #{cask}" + verify_checksum + end + + private + + def expected + @expected ||= cask.sha256 + end + + def computed + @computed ||= Digest::SHA2.file(downloaded_path).hexdigest + end + + def verify_checksum + raise CaskSha256MissingError, "sha256 required: sha256 '#{computed}'" if expected.nil? || expected.empty? + + if expected == computed + odebug "SHA256 checksums match" + else + ohai 'Note: running "brew update" may fix sha256 checksum errors' + raise CaskSha256MismatchError.new(downloaded_path, expected, computed) + end + end end end end diff --git a/Library/Homebrew/cask/lib/hbc/verify/gpg.rb b/Library/Homebrew/cask/lib/hbc/verify/gpg.rb index 6190f67d1..6eb5fc23b 100644 --- a/Library/Homebrew/cask/lib/hbc/verify/gpg.rb +++ b/Library/Homebrew/cask/lib/hbc/verify/gpg.rb @@ -1,60 +1,64 @@ -class Hbc::Verify::Gpg - def self.me?(cask) - cask.gpg - end +module Hbc + module Verify + class Gpg + def self.me?(cask) + cask.gpg + end - attr_reader :cask, :downloaded_path + attr_reader :cask, :downloaded_path - def initialize(cask, downloaded_path, command = Hbc::SystemCommand) - @command = command - @cask = cask - @downloaded_path = downloaded_path - end + def initialize(cask, downloaded_path, command = Hbc::SystemCommand) + @command = command + @cask = cask + @downloaded_path = downloaded_path + end - def available? - return @available unless @available.nil? - @available = self.class.me?(cask) && installed? - end + def available? + return @available unless @available.nil? + @available = self.class.me?(cask) && installed? + end - def installed? - cmd = @command.run("/usr/bin/type", - args: ["-p", "gpg"]) + def installed? + cmd = @command.run("/usr/bin/type", + args: ["-p", "gpg"]) - # if `gpg` is found, return its absolute path - cmd.success? ? cmd.stdout : false - end + # if `gpg` is found, return its absolute path + cmd.success? ? cmd.stdout : false + end - def fetch_sig(force = false) - unversioned_cask = cask.version.is_a?(Symbol) - cached = cask.metadata_subdir("gpg") unless unversioned_cask + def fetch_sig(force = false) + unversioned_cask = cask.version.is_a?(Symbol) + cached = cask.metadata_subdir("gpg") unless unversioned_cask - meta_dir = cached || cask.metadata_subdir("gpg", :now, true) - sig_path = meta_dir.join("signature.asc") + meta_dir = cached || cask.metadata_subdir("gpg", :now, true) + sig_path = meta_dir.join("signature.asc") - curl(cask.gpg.signature, "-o", sig_path.to_s) unless cached || force + curl(cask.gpg.signature, "-o", sig_path.to_s) unless cached || force - sig_path - end + sig_path + end - def import_key - args = if cask.gpg.key_id - ["--recv-keys", cask.gpg.key_id] - elsif cask.gpg.key_url - ["--fetch-key", cask.gpg.key_url.to_s] - end + def import_key + args = if cask.gpg.key_id + ["--recv-keys", cask.gpg.key_id] + elsif cask.gpg.key_url + ["--fetch-key", cask.gpg.key_url.to_s] + end - @command.run!("gpg", args: args) - end + @command.run!("gpg", args: args) + end - def verify - return unless available? - import_key - sig = fetch_sig + def verify + return unless available? + import_key + sig = fetch_sig - ohai "Verifying GPG signature for #{cask}" + ohai "Verifying GPG signature for #{cask}" - @command.run!("gpg", - args: ["--verify", sig, downloaded_path], - print_stdout: true) + @command.run!("gpg", + args: ["--verify", sig, downloaded_path], + print_stdout: true) + end + end end end |
