diff options
Diffstat (limited to 'lib/chef/mixin/which.rb')
-rw-r--r-- | lib/chef/mixin/which.rb | 48 |
1 files changed, 10 insertions, 38 deletions
diff --git a/lib/chef/mixin/which.rb b/lib/chef/mixin/which.rb index a51963b6c2..6f0d096c1b 100644 --- a/lib/chef/mixin/which.rb +++ b/lib/chef/mixin/which.rb @@ -1,6 +1,6 @@ #-- # Author:: Lamont Granquist <lamont@chef.io> -# Copyright:: Copyright 2010-2018, Chef Software Inc. +# Copyright:: Copyright 2010-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,49 +15,21 @@ # See the License for the specific language governing permissions and # limitations under the License. +require "chef-utils/dsl/which" unless defined?(ChefUtils::DSL::Which) +require "chef-utils/dsl/path_sanity" unless defined?(ChefUtils::DSL::PathSanity) + class Chef module Mixin module Which - require_relative "../chef_class" - - def which(*cmds, extra_path: nil, &block) - where(*cmds, extra_path: extra_path, &block).first || false - 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) + Array(extra_path) - cmds.map do |cmd| - paths.map do |path| - filename = Chef.path_to(File.join(path, cmd)) - filename if valid_executable?(filename, &block) - end.compact - end.flatten - end + include ChefUtils::DSL::Which private - # for test stubbing - def env_path - if Chef::Config.target_mode? - Chef.run_context.transport_connection.run_command("echo $PATH").stdout - else - ENV["PATH"] - end - end - - def valid_executable?(filename, &block) - is_executable = - if Chef::Config.target_mode? - connection = Chef.run_context.transport_connection - connection.file(filename).stat[:mode] & 1 && !connection.file(filename).directory? - else - File.executable?(filename) && !File.directory?(filename) - end - return false unless is_executable - - block ? yield(filename) : true + # we dep-inject path sanity into this API for historical reasons + # + # @api private + def __extra_path + ChefUtils::DSL::PathSanity.sane_paths end end end |