Class: Facter::Core::Execution::Posix

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

Overview

Since:

  • 2.0.0

Constant Summary collapse

DEFAULT_SEARCH_PATHS =

Since:

  • 2.0.0

['/sbin', '/usr/sbin'].freeze
ABSOLUTE_PATH_REGEX =

Since:

  • 2.0.0

%r{^/}.freeze
DOUBLE_QUOTED_COMMAND =

Since:

  • 2.0.0

/\A"(.+?)"(?:\s+(.*))?/.freeze
SINGLE_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, #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



30
31
32
# File 'lib/facter/custom_facts/core/execution/posix.rb', line 30

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

#expand_command(command) ⇒ Object

Since:

  • 2.0.0



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/facter/custom_facts/core/execution/posix.rb', line 37

def expand_command(command)
  exe = nil
  args = nil

  match = command.match(DOUBLE_QUOTED_COMMAND) || command.match(SINGLE_QUOTED_COMMAND)
  if match
    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



9
10
11
12
13
14
# File 'lib/facter/custom_facts/core/execution/posix.rb', line 9

def search_paths
  # Make sure custom_facts is usable even for non-root users. Most commands
  # in /sbin (like ifconfig) can be run as non privileged users as
  # long as they do not modify anything - which we do not do with custom_facts
  ENV['PATH'].split(File::PATH_SEPARATOR) + DEFAULT_SEARCH_PATHS
end

#which(bin) ⇒ Object

Since:

  • 2.0.0



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/facter/custom_facts/core/execution/posix.rb', line 16

def which(bin)
  if absolute_path?(bin)
    return bin if File.executable?(bin) && FileTest.file?(bin)
  else
    search_paths.each do |dir|
      dest = File.join(dir, bin)
      return dest if File.executable?(dest) && FileTest.file?(dest)
    end
  end
  nil
end