Sha256: 252a58829391fff30d1972d9192f8c70f7738035380fa50ce62334b43eed402f

Contents?: true

Size: 1.3 KB

Versions: 75

Compression:

Stored size: 1.3 KB

Contents

class Facter::Core::Execution::Posix < Facter::Core::Execution::Base

  DEFAULT_SEARCH_PATHS = ['/sbin', '/usr/sbin']

  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

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

  ABSOLUTE_PATH_REGEX = %r{^/}

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

  DOUBLE_QUOTED_COMMAND = /^"(.+?)"(?:\s+(.*))?/
  SINGLE_QUOTED_COMMAND = /^'(.+?)'(?:\s+(.*))?/

  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
end

Version data entries

75 entries across 75 versions & 2 rubygems

Version Path
facter-2.5.7 lib/facter/core/execution/posix.rb
facter-2.5.7-x86-mingw32 lib/facter/core/execution/posix.rb
facter-2.5.7-x64-mingw32 lib/facter/core/execution/posix.rb
facter-2.5.7-universal-darwin lib/facter/core/execution/posix.rb
facter-2.5.6-x86-mingw32 lib/facter/core/execution/posix.rb
facter-2.5.6-x64-mingw32 lib/facter/core/execution/posix.rb
facter-2.5.6-universal-darwin lib/facter/core/execution/posix.rb
facter-2.5.6 lib/facter/core/execution/posix.rb
facter-2.5.5-x86-mingw32 lib/facter/core/execution/posix.rb
facter-2.5.5-x64-mingw32 lib/facter/core/execution/posix.rb
facter-2.5.5-universal-darwin lib/facter/core/execution/posix.rb
facter-2.5.5 lib/facter/core/execution/posix.rb
facter-2.5.4-x86-mingw32 lib/facter/core/execution/posix.rb
facter-2.5.4-x64-mingw32 lib/facter/core/execution/posix.rb
facter-2.5.4-universal-darwin lib/facter/core/execution/posix.rb
facter-2.5.4 lib/facter/core/execution/posix.rb
bolt-0.24.0 vendored/facter/lib/facter/core/execution/posix.rb
bolt-0.23.0 vendored/facter/lib/facter/core/execution/posix.rb
bolt-0.22.0 vendored/facter/lib/facter/core/execution/posix.rb
bolt-0.21.8 vendored/facter/lib/facter/core/execution/posix.rb