module Chef::Mixin::Which

Public Instance Methods

where(*cmds, extra_path: nil, &block) click to toggle source
# File lib/chef/mixin/which.rb, line 27
def where(*cmds, extra_path: nil, &block)
  # NOTE: unnecessarily duplicates function of path_sanity
  extra_path ||= [ "/bin", "/usr/bin", "/sbin", "/usr/sbin" ]
  paths = env_path.split(File::PATH_SEPARATOR) + Array(extra_path)
  cmds.map do |cmd|
    paths.map do |path|
      filename = Chef.path_to(File.join(path, cmd))
      filename if valid_executable?(filename, &block)
    end.compact
  end.flatten
end
which(*cmds, extra_path: nil, &block) click to toggle source
# File lib/chef/mixin/which.rb, line 23
def which(*cmds, extra_path: nil, &block)
  where(*cmds, extra_path: extra_path, &block).first || false
end

Private Instance Methods

env_path() click to toggle source

for test stubbing

# File lib/chef/mixin/which.rb, line 42
def env_path
  if Chef::Config.target_mode?
    Chef.run_context.transport_connection.run_command("echo $PATH").stdout
  else
    ENV["PATH"]
  end
end
valid_executable?(filename) { |filename| ... } click to toggle source
# File lib/chef/mixin/which.rb, line 50
def valid_executable?(filename, &block)
  is_executable =
    if Chef::Config.target_mode?
      connection = Chef.run_context.transport_connection
      connection.file(filename).stat[:mode] & 1 && !connection.file(filename).directory?
    else
      File.executable?(filename) && !File.directory?(filename)
    end
  return false unless is_executable
  block ? yield(filename) : true
end