class Facter::Core::Execution::Posix

Constants

ABSOLUTE_PATH_REGEX
DEFAULT_SEARCH_PATHS
DOUBLE_QUOTED_COMMAND
SINGLE_QUOTED_COMMAND

Public Instance Methods

absolute_path?(path) click to toggle source
# File lib/facter/custom_facts/core/execution/posix.rb, line 28
def absolute_path?(path)
  !!(path =~ ABSOLUTE_PATH_REGEX)
end
expand_command(command) click to toggle source
# File lib/facter/custom_facts/core/execution/posix.rb, line 35
def expand_command(command)
  exe = nil
  args = nil

  if (match = (command.match(DOUBLE_QUOTED_COMMAND) || command.match(SINGLE_QUOTED_COMMAND)))
    exe, args = match.captures
  else
    exe, args = command.split(/ /, 2)
  end

  return unless exe && (expanded = which(exe))

  expanded = "'#{expanded}'" if /\s/.match?(expanded)
  expanded << " #{args}" if args

  expanded
end
search_paths() click to toggle source
# File lib/facter/custom_facts/core/execution/posix.rb, line 7
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) click to toggle source
# File lib/facter/custom_facts/core/execution/posix.rb, line 14
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