Class: Inspec::Resources::Brew

Inherits:
PkgManagement show all
Defined in:
lib/inspec/resources/package.rb

Overview

MacOS / Darwin implementation

Instance Attribute Summary

Attributes inherited from PkgManagement

#inspec

Instance Method Summary collapse

Methods inherited from PkgManagement

#initialize, #missing_requirements

Constructor Details

This class inherits a constructor from Inspec::Resources::PkgManagement

Instance Method Details

#info(package_name) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/inspec/resources/package.rb', line 247

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",
    latest_version: pkg["versions"]["stable"],
    only_version_no: pkg["installed"][0]["version"],
  }
rescue JSON::ParserError => e
  raise Inspec::Exceptions::ResourceFailed,
        "Failed to parse JSON from `brew` command. Error: #{e}"
end

#latest_version(package_name) ⇒ Object



273
274
275
# File 'lib/inspec/resources/package.rb', line 273

def latest_version(package_name)
  nil
end