Sha256: 8fbd2b94dcfb2740fca1fc134fa244e72c01edae5e0e5d3032b2495e44b66fb2

Contents?: true

Size: 2 KB

Versions: 39

Compression:

Stored size: 2 KB

Contents

module Facter
  module Core
    module Execution
      class Windows < Facter::Core::Execution::Base
        def search_paths
          ENV['PATH'].split(File::PATH_SEPARATOR)
        end

        DEFAULT_COMMAND_EXTENSIONS = %w[.COM .EXE .BAT .CMD].freeze

        def which(bin)
          if absolute_path?(bin)
            return bin if File.executable?(bin)
          else
            search_paths.each do |dir|
              dest = File.join(dir, bin)
              dest.gsub!(File::SEPARATOR, File::ALT_SEPARATOR)
              if File.extname(dest).empty?
                exts = ENV['PATHEXT']
                exts = exts ? exts.split(File::PATH_SEPARATOR) : DEFAULT_COMMAND_EXTENSIONS
                exts.each do |ext|
                  destext = dest + ext
                  return destext if File.executable?(destext)
                end
              end
              return dest if File.executable?(dest)
            end
          end
          nil
        end

        slash = '[\\\\/]'
        name = '[^\\\\/]+'
        ABSOLUTE_PATH_REGEX =
          /^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))/i.freeze

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

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

        def expand_command(command)
          exe = nil
          args = nil

          if (match = command.match(DOUBLE_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

        def execute(command, options = {})
          expand = options.fetch(:expand, true)
          raise ArgumentError.new, 'Unsupported argument on Windows expand with value false' unless expand

          super(command, options)
        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/windows.rb
facter-4.1.0 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.52 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.51 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.50 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.49 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.48 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.47 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.46 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.44 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.43 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.42 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.41 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.40 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.39 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.38 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.37 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.36 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.35 lib/facter/custom_facts/core/execution/windows.rb
facter-4.0.34 lib/facter/custom_facts/core/execution/windows.rb