Sha256: 937537733c84cfd31c59e473eab59a1bb8950f2770ad0858d91cf6d5af9a85f2

Contents?: true

Size: 1.68 KB

Versions: 7

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

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 = /\A"(.+?)"(?:\s+(.*))?/.freeze
        SINGLE_QUOTED_COMMAND = /\A'(.+?)'(?:\s+(.*))?/.freeze

        def expand_command(command)
          exe = nil
          args = nil

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

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

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

          expanded
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
facter-4.10.0 lib/facter/custom_facts/core/execution/posix.rb
facter-4.9.0 lib/facter/custom_facts/core/execution/posix.rb
facter-4.8.0 lib/facter/custom_facts/core/execution/posix.rb
facter-4.7.1 lib/facter/custom_facts/core/execution/posix.rb
facter-4.7.0 lib/facter/custom_facts/core/execution/posix.rb
facter-4.6.1 lib/facter/custom_facts/core/execution/posix.rb
facter-4.6.0 lib/facter/custom_facts/core/execution/posix.rb