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

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

Constants inherited from Base

Base::DEFAULT_EXECUTION_TIMEOUT, Base::STDERR_MESSAGE, Base::VALID_OPTIONS

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



43
44
45
# File 'lib/facter/custom_facts/core/execution/windows.rb', line 43

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

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

Raises:

  • (ArgumentError.new)

Since:

  • 2.0.0



68
69
70
71
72
73
# File 'lib/facter/custom_facts/core/execution/windows.rb', line 68

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/facter/custom_facts/core/execution/windows.rb', line 49

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.dup
  expanded = +"\"#{expanded}\"" if /\s+/.match?(expanded)
  expanded << " #{args}" if args

  expanded
end

#search_pathsObject

Since:

  • 2.0.0



7
8
9
# File 'lib/facter/custom_facts/core/execution/windows.rb', line 7

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

#which(bin) ⇒ Object

Since:

  • 2.0.0



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/facter/custom_facts/core/execution/windows.rb', line 13

def which(bin)
  # `echo` is allowed for facter 3.x compatibility, otherwise
  # all commands much be found on the PATH or absolute.
  return bin if /^echo$/i.match?(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