diff options
| author | Lamont Granquist <lamont@scriptkiddie.org> | 2013-06-20 16:33:36 -0700 |
|---|---|---|
| committer | Lamont Granquist <lamont@scriptkiddie.org> | 2013-06-20 16:33:36 -0700 |
| commit | b87f5b2e8721f13e2a32dc1c3be7d749af3720d6 (patch) | |
| tree | 362b457d2e541a4e2c30cc9eb6486c9619871804 | |
| parent | 008ef605d250aafc743bef2444f53b568cc0a169 (diff) | |
| download | chef-lcg/test-for-erubis.tar.gz | |
comments and reararngementlcg/test-for-erubis
| -rw-r--r-- | lib/chef/mixin/which.rb | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/lib/chef/mixin/which.rb b/lib/chef/mixin/which.rb index 993bad9847..373da5ca92 100644 --- a/lib/chef/mixin/which.rb +++ b/lib/chef/mixin/which.rb @@ -20,15 +20,25 @@ class Chef module Mixin module Which - def which(cmd) - exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] - ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| - exts.each { |ext| - exe = File.join(path, "#{cmd}#{ext}") - return exe if File.executable? exe - } + # Acts like a shell 'which' command in ruby. + # + # === Parameters + # command<String>:: the command to search the PATH for + # + # === Returns + # path<String> or nil + def which(command) + + # Windows fills PATHEXT with things like ".COM", ".EXE", ".BAT", etc + extlist = ENV['PATHEXT'].split(';')) if ENV['PATHEXT'] + commandlist = extlist ? extlist.map { |e| "#{command}#{e}" } : [ command ] + ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir| + commandpaths = commandlist.map { |cmd| File.join(dir, cmd) } + commandpaths.each do |path| + return path if File.executable? path + end end - return nil + nil end end |
