module ChefUtils::DSL::Which

def __extra_path

Other tags:
    Api: - private
def __extra_path
  nil
end

def __valid_executable?(filename, &block)

Other tags:
    Api: - private
def __valid_executable?(filename, &block)
  is_executable =
    if __transport_connection
      __transport_connection.file(filename).stat[:mode] & 1 && !__transport_connection.file(filename).directory?
    else
      File.executable?(filename) && !File.directory?(filename)
    end
  return false unless is_executable
  block ? yield(filename) : true
end

def where(*cmds, extra_path: nil, &block)

Returns:
  • (String) - the first match

Parameters:
  • array (String, Array) -- of extra paths to search through
  • list (Array) -- of commands to search for

Other tags:
    Example: Find all the python executables, searching through the system PATH plus additionally -
def where(*cmds, extra_path: nil, &block)
  extra_path ||= __extra_path
  paths = __env_path.split(File::PATH_SEPARATOR) + Array(extra_path)
  paths.uniq!
  cmds.map do |cmd|
    paths.map do |path|
      filename = File.join(path, cmd)
      filename if __valid_executable?(filename, &block)
    end.compact
  end.flatten
end

def which(*cmds, extra_path: nil, &block)

Returns:
  • (String) - the first match

Parameters:
  • array (String, Array) -- of extra paths to search through
  • list (Array) -- of commands to search for

Other tags:
    Example: Find the most appropriate python executable, searching through the system PATH -
def which(*cmds, extra_path: nil, &block)
  where(*cmds, extra_path: extra_path, &block).first || false
end