Class: Facter::Core::Execution::Windows
- Defined in:
- lib/custom_facts/core/execution/windows.rb
Overview
Constant Summary collapse
- DEFAULT_COMMAND_EXTENSIONS =
%w[.COM .EXE .BAT .CMD].freeze
- ABSOLUTE_PATH_REGEX =
/^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))/i.freeze
- DOUBLE_QUOTED_COMMAND =
/^"(.+?)"(?:\s+(.*))?/.freeze
Instance Method Summary collapse
- #absolute_path?(path) ⇒ Boolean
- #execute(command, options = {}) ⇒ Object
- #expand_command(command) ⇒ Object
- #search_paths ⇒ Object
- #which(bin) ⇒ Object
Methods inherited from Base
Instance Method Details
#absolute_path?(path) ⇒ Boolean
37 38 39 |
# File 'lib/custom_facts/core/execution/windows.rb', line 37 def absolute_path?(path) !!(path =~ ABSOLUTE_PATH_REGEX) end |
#execute(command, options = {}) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/custom_facts/core/execution/windows.rb', line 61 def execute(command, = {}) = .fetch(:expand, true) raise ArgumentError.new, 'Unsupported argument on Windows expand with value false' unless super(command, ) end |
#expand_command(command) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/custom_facts/core/execution/windows.rb', line 43 def (command) exe = nil args = nil if (match = command.match(DOUBLE_QUOTED_COMMAND)) exe, args = match.captures else exe, args = command.split(/ /, 2) end return unless exe && ( = which(exe)) = "\"#{}\"" if =~ /\s+/ << " #{args}" if args end |
#search_paths ⇒ Object
5 6 7 |
# File 'lib/custom_facts/core/execution/windows.rb', line 5 def search_paths ENV['PATH'].split(File::PATH_SEPARATOR) end |
#which(bin) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/custom_facts/core/execution/windows.rb', line 11 def which(bin) if absolute_path?(bin) return bin if File.executable?(bin) else search_paths.each do |dir| dest = File.join(dir, bin) dest.gsub!(File::SEPARATOR, File::ALT_SEPARATOR) if File.extname(dest).empty? exts = ENV['PATHEXT'] exts = exts ? exts.split(File::PATH_SEPARATOR) : DEFAULT_COMMAND_EXTENSIONS exts.each do |ext| destext = dest + ext return destext if File.executable?(destext) end end return dest if File.executable?(dest) end end nil end |