Module: ChefUtils::DSL::Which
Instance Method Summary collapse
-
#where(*cmds, prepend_path: nil, extra_path: nil, &block) ⇒ String
Lookup all the instances of an an executable that can be found through the systems search PATH.
-
#which(*cmds, prepend_path: nil, extra_path: nil, &block) ⇒ String
Lookup an executable through the systems search PATH.
Instance Method Details
#where(*cmds, prepend_path: nil, extra_path: nil, &block) ⇒ String
Lookup all the instances of an an executable that can be found through the systems search PATH. Allows specifying an array of executables to look for. All the instances of the first executable that is found will be returned first. The extra_path will override any default extra_paths which are added (allowing the user to pass an empty array to remove them).
When passed a block the block will be called with the full pathname of any executables which are found, and the block should return truthy or falsey values to further filter the executable based on arbitrary criteria.
This helper can be used in target mode in chef or with train using the appropriate wiring externally.
cmds = where("platform-python", "python", "python3", "python2", "python2.7", extra_path: "/usr/libexec") do |f|
shell_out("#{f} -c 'import dnf'").exitstatus == 0
end
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/chef-utils/dsl/which.rb', line 81 def where(*cmds, prepend_path: nil, extra_path: nil, &block) extra_path ||= __extra_path paths = Array(prepend_path) + __env_path.split(File::PATH_SEPARATOR) + Array(extra_path) paths.uniq! exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [] exts.unshift("") cmds.map do |cmd| paths.map do |path| exts.map do |ext| filename = File.join(path, "#{cmd}#{ext}") filename if __valid_executable?(filename, &block) end.compact end end.flatten end |
#which(*cmds, prepend_path: nil, extra_path: nil, &block) ⇒ String
Lookup an executable through the systems search PATH. Allows specifying an array of executables to look for. The first executable that is found, along any path entry, will be the preferred one and returned first. The extra_path will override any default extra_paths which are added (allowing the user to pass an empty array to remove them).
When passed a block the block will be called with the full pathname of any executables which are found, and the block should return truthy or falsey values to further filter the executable based on arbitrary criteria.
This is syntactic sugar for ‘where(…).first`
This helper can be used in target mode in chef or with train using the appropriate wiring externally.
cmd = which("platform-python", "python", "python3", "python2", "python2.7", extra_path: "/usr/libexec") do |f|
shell_out("#{f} -c 'import dnf'").exitstatus == 0
end
53 54 55 |
# File 'lib/chef-utils/dsl/which.rb', line 53 def which(*cmds, prepend_path: nil, extra_path: nil, &block) where(*cmds, prepend_path: prepend_path, extra_path: extra_path, &block).first || false end |