Method: Puppet::Util.which

Defined in:
lib/vendor/puppet/util.rb

.which(bin) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/vendor/puppet/util.rb', line 170

def which(bin)
  if absolute_path?(bin)
    return bin if FileTest.file? bin and FileTest.executable? bin
  else
    ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir|
      begin
        dest = File.expand_path(File.join(dir, bin))
        if Puppet.features.microsoft_windows? && File.extname(dest).empty?
          exts = ENV['PATHEXT']
          exts = exts ? exts.split(File::PATH_SEPARATOR) : %w[.COM .EXE .BAT .CMD]
          exts.each do |ext|
            destext = File.expand_path(dest + ext)
            return destext if FileTest.file? destext and FileTest.executable? destext
          end
        end
        return dest if FileTest.file? dest and FileTest.executable? dest
      rescue ArgumentError => e
        raise unless e.to_s =~ /doesn't exist|can't find user/
        # ...otherwise, we just skip the non-existent entry, and do nothing.
      end
    end
  end
  nil
end