Module: ChefUtils::DSL::Which

Extended by:
Which
Includes:
Internal
Included in:
ChefUtils, Which
Defined in:
lib/chef-utils/dsl/which.rb

Instance Method Summary collapse

Instance Method Details

#where(*cmds, extra_path: nil, &block) ⇒ String

Lookup all the instances of an an executable that can be found through the systems search PATH. Allows specifying an array of executables to look for. All the instances of the first executable that is found will be returned first. The extra_path will override any default extra_paths which are added (allowing the user to pass an empty array to remove them).

When passed a block the block will be called with the full pathname of any executables which are found, and the block should return truthy or falsey values to further filter the executable based on arbitrary criteria.

This helper can be used in target mode in chef or with train using the appropriate wiring externally.

cmds = where("platform-python", "python", "python3", "python2", "python2.7", extra_path: "/usr/libexec") do |f|
  shell_out("#{f} -c 'import dnf'").exitstatus == 0
end

Examples:

Find all the python executables, searching through the system PATH plus additionally

the "/usr/libexec" directory, which have the dnf libraries installed and available.

Parameters:

  • list (Array<String>)

    of commands to search for

  • array (String, Array<String>)

    of extra paths to search through

Returns:

  • (String)

    the first match



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chef-utils/dsl/which.rb', line 79

def where(*cmds, extra_path: nil, &block)
  extra_path ||= __extra_path
  paths = __env_path.split(File::PATH_SEPARATOR) + Array(extra_path)
  paths.uniq!
  exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : []
  exts.unshift("")
  cmds.map do |cmd|
    paths.map do |path|
      exts.map do |ext|
        filename = File.join(path, "#{cmd}#{ext}")
        filename if __valid_executable?(filename, &block)
      end.compact
    end
  end.flatten
end

#which(*cmds, extra_path: nil, &block) ⇒ String

Lookup an executable through the systems search PATH. Allows specifying an array of executables to look for. The first executable that is found, along any path entry, will be the preferred one and returned first. The extra_path will override any default extra_paths which are added (allowing the user to pass an empty array to remove them).

When passed a block the block will be called with the full pathname of any executables which are found, and the block should return truthy or falsey values to further filter the executable based on arbitrary criteria.

This is syntactic sugar for ‘where(…).first`

This helper can be used in target mode in chef or with train using the appropriate wiring externally.

cmd = which("platform-python", "python", "python3", "python2", "python2.7", extra_path: "/usr/libexec") do |f|
  shell_out("#{f} -c 'import dnf'").exitstatus == 0
end

Examples:

Find the most appropriate python executable, searching through the system PATH

plus additionally the "/usr/libexec" directory, which has the dnf libraries
installed and available.

Parameters:

  • list (Array<String>)

    of commands to search for

  • array (String, Array<String>)

    of extra paths to search through

Returns:

  • (String)

    the first match



52
53
54
# File 'lib/chef-utils/dsl/which.rb', line 52

def which(*cmds, extra_path: nil, &block)
  where(*cmds, extra_path: extra_path, &block).first || false
end