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

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

Constant Summary collapse

DEFAULT_SEARCH_PATHS =
['/sbin', '/usr/sbin']
ABSOLUTE_PATH_REGEX =
%r{^/}
DOUBLE_QUOTED_COMMAND =
/^"(.+?)"(?:\s+(.*))?/
SINGLE_QUOTED_COMMAND =
/^'(.+?)'(?:\s+(.*))?/

Instance Method Summary collapse

Methods inherited from Base

#execute, #with_env

Instance Method Details

#absolute_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/facter/core/execution/posix.rb', line 26

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

#expand_command(command) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/facter/core/execution/posix.rb', line 33

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

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

    return expanded
  end
end

#search_pathsObject



5
6
7
8
9
10
# File 'lib/facter/core/execution/posix.rb', line 5

def search_paths
  # Make sure facter 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 facter
  ENV['PATH'].split(File::PATH_SEPARATOR) + DEFAULT_SEARCH_PATHS
end

#which(bin) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/facter/core/execution/posix.rb', line 12

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