Class: Inspec::Resources::Brew

Inherits:
PkgManagement show all
Defined in:
lib/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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/resources/package.rb', line 195

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