Sha256: dd8752fdcbca71fa7c6ad87b26b018f0c0628fe8732dcff2989d2af12a82c000

Contents?: true

Size: 1.6 KB

Versions: 39

Compression:

Stored size: 1.6 KB

Contents

module Facter
  module Core
    module Execution
      class Posix < Facter::Core::Execution::Base
        DEFAULT_SEARCH_PATHS = ['/sbin', '/usr/sbin'].freeze

        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

        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

        ABSOLUTE_PATH_REGEX = %r{^/}.freeze

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

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

        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 expanded =~ /\s/
          expanded << " #{args}" if args

          expanded
        end
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
facter-4.1.1 lib/facter/custom_facts/core/execution/posix.rb
facter-4.1.0 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.52 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.51 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.50 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.49 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.48 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.47 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.46 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.44 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.43 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.42 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.41 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.40 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.39 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.38 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.37 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.36 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.35 lib/facter/custom_facts/core/execution/posix.rb
facter-4.0.34 lib/facter/custom_facts/core/execution/posix.rb