module PoiseLanguages::Utils::Which

Replacement module for Chef::Mixin::Which with a slight improvement.

@since 1.0.0 @see Which#which

Public Instance Methods

which(cmd, extra_path: %w{/bin /usr/bin /sbin /usr/sbin}, path: nil) click to toggle source

A replacement for Chef::Mixin::Which#which that allows using something other than an environment variable if needed.

@param cmd [String] Executable to search for. @param extra_path [Array<String>] Extra directories to always search. @param path [String, nil] Replacement $PATH value. @return [String, false]

# File lib/poise_languages/utils/which.rb, line 34
def which(cmd, extra_path: %w{/bin /usr/bin /sbin /usr/sbin}, path: nil)
  # If it was already absolute, just return that.
  return cmd if cmd =~ /^(\/|([a-z]:)?\\)/i
  # Allow passing something other than the real env var.
  path ||= ENV['PATH']
  # Based on Chef::Mixin::Which#which
  # Copyright 2010-2017, Chef Softare, Inc.
  paths = path.split(File::PATH_SEPARATOR) + extra_path
  paths.each do |candidate_path|
    filename = ::File.join(candidate_path, cmd)
    return filename if ::File.executable?(filename)
  end
  false
end