class Inspec::Resources::Brew

MacOS / Darwin implementation

Public Instance Methods

info(package_name) click to toggle source
# File lib/inspec/resources/package.rb, line 201
def info(package_name)
  brew_path = inspec.command("brew").exist? ? "brew" : "/usr/local/bin/brew"
  cmd = inspec.command("#{brew_path} info --json=v1 #{package_name}")

  # If no available formula exists, then `brew` will exit non-zero
  return {} if cmd.exit_status.to_i != 0

  pkg = JSON.parse(cmd.stdout)[0]

  # If package exists but is not installed, then `brew` output will not
  # contain `pkg['installed'][0]['version']
  return {} unless pkg.dig("installed", 0, "version")

  {
    name: pkg["name"],
    installed: true,
    version: pkg["installed"][0]["version"],
    type: "brew",
  }
rescue JSON::ParserError => e
  raise Inspec::Exceptions::ResourceFailed,
        "Failed to parse JSON from `brew` command. Error: #{e}"
end