Class: ConfigCurator::PackageLookup

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/config_curator/package_lookup.rb

Overview

Lookup if a package is installed on the system. See TOOLS for supported package tools. If #tool is not explicitly set, it will try and choose one automatically.

Examples:

Lookup a package

PackageLookup.new.installed? 'ruby' #=> true

Lookup a package using pacman

PackageLookup.new(tool: :pacman).installed? 'ruby' #=> true

Defined Under Namespace

Classes: LookupFailed

Constant Summary collapse

TOOLS =

Default list of supported package tools.

See Also:

{
  dpkg: 'dpkg',
  pacman: 'pacman',
  pkgng: 'pkg'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tool: nil) ⇒ PackageLookup


28
29
30
# File 'lib/config_curator/package_lookup.rb', line 28

def initialize(tool: nil)
  self.tool = tool
end

Instance Attribute Details

#toolSymbol

The package tool to use for this instance.


41
42
43
# File 'lib/config_curator/package_lookup.rb', line 41

def tool
  @tool
end

#toolsHash

Package tools that support package lookup ordered by preference. Each key is an identifier and each value is the command to check for.


35
36
37
# File 'lib/config_curator/package_lookup.rb', line 35

def tools
  @tools
end

Instance Method Details

#installed?(package) ⇒ Boolean

Checks if package is installed.


54
55
56
57
58
59
60
61
# File 'lib/config_curator/package_lookup.rb', line 54

def installed?(package)
  fail LookupFailed, 'No supported package tool found.' if tool.nil?

  cmd = tools[tool]
  fail LookupFailed, "Package tool '#{cmd}' not found." if command?(cmd).nil?

  send tool, package
end