diff options
author | Ho-Sheng Hsiao <hosheng.hsiao@gmail.com> | 2012-04-02 17:48:16 -0400 |
---|---|---|
committer | Ho-Sheng Hsiao <hosh@opscode.com> | 2012-05-10 14:51:43 -0400 |
commit | 146de90bee9a19f97a2b13595d39cbd99cb7eb77 (patch) | |
tree | 22d6703d8cc9dc64d98bd56316e336327ba56ba3 /lib | |
parent | b9df898902417866409e74d1e6098ca3dbe01f3d (diff) | |
download | mixlib-shellout-146de90bee9a19f97a2b13595d39cbd99cb7eb77.tar.gz |
[CHEF-2994][WINDOWS] Fixed Utils.which()
- Factored out Utils.should_run_under_cmd? (simple case)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mixlib/shellout/windows.rb | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/lib/mixlib/shellout/windows.rb b/lib/mixlib/shellout/windows.rb index 942f377..1703236 100644 --- a/lib/mixlib/shellout/windows.rb +++ b/lib/mixlib/shellout/windows.rb @@ -165,6 +165,8 @@ module Mixlib IS_BATCH_FILE = /\.bat"?$|\.cmd"?$/i def command_to_run(command) + return _run_under_cmd(command) if Utils.should_run_under_cmd?(command) + candidate = candidate_executable_for_command(command) # Don't do searching for empty commands. Let it fail when it runs. @@ -174,18 +176,27 @@ module Mixlib exe = Utils.find_executable(candidate) exe = Utils.which(unquoted_executable_path(command)) if exe.nil? && exe !~ /[\\\/]/ + # Batch files MUST use cmd; and if we couldn't find the command we're looking for, + # we assume it must be a cmd builtin. if exe.nil? || exe =~ IS_BATCH_FILE - # Batch files MUST use cmd; and if we couldn't find the command we're looking for, we assume it must be a cmd builtin. - # - # cmd does not parse multiple quotes well unless the whole thing is wrapped up in quotes. - # https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4837859 - # http://ss64.com/nt/syntax-esc.html - [ ENV['COMSPEC'], "cmd /c \"#{command}\"" ] + _run_under_cmd(command) else - [ exe, command ] + _run_directly(command, exe) end end + + # cmd does not parse multiple quotes well unless the whole thing is wrapped up in quotes. + # https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4837859 + # http://ss64.com/nt/syntax-esc.html + def _run_under_cmd(command) + [ ENV['COMSPEC'], "cmd /c \"#{command}\"" ] + end + + def _run_directly(command, exe) + [ exe, command ] + end + def unquoted_executable_path(command) command[0,command.index(/\s/) || command.length] end @@ -217,6 +228,13 @@ module Mixlib end module Utils + # api: semi-private + # If there are special characters parsable by cmd.exe (such as file redirection), then + # this method should return true. + def self.should_run_under_cmd?(command) + !!(command =~ /[%&|<>{}@^]/) + end + def self.pathext @pathext ||= ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') + [''] : [''] end @@ -224,10 +242,8 @@ module Mixlib # which() mimicks the Unix which command # FIXME: it is not working def self.which(cmd) - return nil # noop - ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| - exe = find_executable("#{path}/${cmd}") # This looks like it got disabled + exe = find_executable("#{path}/#{cmd}") return exe if exe end return nil |