Class: Facter::Core::Execution::Windows

Inherits:
Base
  • Object
show all
Defined in:
lib/facter/custom_facts/core/execution/windows.rb

Overview

Since:

  • 2.0.0

Constant Summary collapse

DEFAULT_COMMAND_EXTENSIONS =

Since:

  • 2.0.0

%w[.COM .EXE .BAT .CMD].freeze
ABSOLUTE_PATH_REGEX =

Since:

  • 2.0.0

/^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))/i.freeze
DOUBLE_QUOTED_COMMAND =

Since:

  • 2.0.0

/^"(.+?)"(?:\s+(.*))?/.freeze

Constants inherited from Base

Base::STDERR_MESSAGE

Instance Method Summary collapse

Methods inherited from Base

#execute_command, #initialize, #with_env

Constructor Details

This class inherits a constructor from Facter::Core::Execution::Base

Instance Method Details

#absolute_path?(path) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 2.0.0



37
38
39
# File 'lib/facter/custom_facts/core/execution/windows.rb', line 37

def absolute_path?(path)
  !!(path =~ ABSOLUTE_PATH_REGEX)
end

#execute(command, options = {}) ⇒ Object

Raises:

  • (ArgumentError.new)

Since:

  • 2.0.0



61
62
63
64
65
66
# File 'lib/facter/custom_facts/core/execution/windows.rb', line 61

def execute(command, options = {})
  expand = options.fetch(:expand, true)
  raise ArgumentError.new, 'Unsupported argument on Windows expand with value false' unless expand

  super(command, options)
end

#expand_command(command) ⇒ Object

Since:

  • 2.0.0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/facter/custom_facts/core/execution/windows.rb', line 43

def expand_command(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 && (expanded = which(exe))

  expanded = "\"#{expanded}\"" if expanded =~ /\s+/
  expanded << " #{args}" if args

  expanded
end

#search_pathsObject

Since:

  • 2.0.0



5
6
7
# File 'lib/facter/custom_facts/core/execution/windows.rb', line 5

def search_paths
  ENV['PATH'].split(File::PATH_SEPARATOR)
end

#which(bin) ⇒ Object

Since:

  • 2.0.0



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/facter/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