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',
  brew: 'brew'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tool: nil) ⇒ PackageLookup

Returns a new instance of PackageLookup.



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

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

Instance Attribute Details

#toolSymbol

The package tool to use for this instance.

Returns:

  • (Symbol)

    tool to use



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

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.

Returns:

  • (Hash)

    hash of supported package tools



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

def tools
  @tools
end

Instance Method Details

#installed?(package) ⇒ Boolean

Checks if package is installed.

Parameters:

  • package (String)

    package name to check

Returns:

  • (Boolean)

    if package is installed



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

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