Class: Facter::Core::Execution::Posix
- Defined in:
- lib/facter/custom_facts/core/execution/posix.rb
Overview
Constant Summary collapse
- DEFAULT_SEARCH_PATHS =
['/sbin', '/usr/sbin'].freeze
- ABSOLUTE_PATH_REGEX =
%r{^/}.freeze
- DOUBLE_QUOTED_COMMAND =
/^"(.+?)"(?:\s+(.*))?/.freeze
- SINGLE_QUOTED_COMMAND =
/^'(.+?)'(?:\s+(.*))?/.freeze
Constants inherited from Base
Instance Method Summary collapse
- #absolute_path?(path) ⇒ Boolean
- #expand_command(command) ⇒ Object
- #search_paths ⇒ Object
- #which(bin) ⇒ Object
Methods inherited from Base
Instance Method Details
#absolute_path?(path) ⇒ Boolean
28 29 30 |
# File 'lib/facter/custom_facts/core/execution/posix.rb', line 28 def absolute_path?(path) !!(path =~ ABSOLUTE_PATH_REGEX) end |
#expand_command(command) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/facter/custom_facts/core/execution/posix.rb', line 35 def (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 && ( = which(exe)) = "'#{}'" if =~ /\s/ << " #{args}" if args end |
#search_paths ⇒ Object
7 8 9 10 11 12 |
# 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) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# 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 |