summaryrefslogtreecommitdiff
path: root/lib/chef/mixin/which.rb
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2017-02-06 11:46:20 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2017-02-07 11:27:09 -0800
commite1f1ba4c9c5a802c0f50b4d5e53b26a5da982c20 (patch)
tree9360411b39daf533c8f49e28b197b0705b24ae1c /lib/chef/mixin/which.rb
parentb1d3d41697b99ea8d4615c6cca5776c9276bc0be (diff)
downloadchef-e1f1ba4c9c5a802c0f50b4d5e53b26a5da982c20.tar.gz
rhel7 / dnf 2.0 fixes / improved errors
- fixes for dnf 2.0 / rhel7 - improved error messages from python helper (gives python stack traces instead of just EPIPE all the time) - improved which/where interface in the which mixin. Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'lib/chef/mixin/which.rb')
-rw-r--r--lib/chef/mixin/which.rb26
1 files changed, 19 insertions, 7 deletions
diff --git a/lib/chef/mixin/which.rb b/lib/chef/mixin/which.rb
index 4fa79eeccb..92934ce485 100644
--- a/lib/chef/mixin/which.rb
+++ b/lib/chef/mixin/which.rb
@@ -18,15 +18,27 @@
class Chef
module Mixin
module Which
- def which(cmd, extra_path: nil)
+ def which(*cmds, extra_path: nil, &block)
+ where(*cmds, extra_path: extra_path, &block).first
+ end
+
+ 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) + extra_path
- paths.each do |path|
- filename = Chef.path_to(File.join(path, cmd))
- return filename if File.executable?(filename)
- end
- false
+ cmds.map do |cmd|
+ paths = ENV["PATH"].split(File::PATH_SEPARATOR) + extra_path
+ paths.map do |path|
+ filename = Chef.path_to(File.join(path, cmd))
+ filename if valid_executable?(filename, &block)
+ end.compact
+ end.flatten
+ end
+
+ private
+
+ def valid_executable?(filename, &block)
+ return false unless File.executable?(filename) && !File.directory?(filename)
+ block ? yield(filename) : true
end
end
end