Sha256: f45939f3e08b332854df18f7dd8bf4e0c6920974621ed6b95bdde077f9e84678

Contents?: true

Size: 1.43 KB

Versions: 121

Compression:

Stored size: 1.43 KB

Contents

class Facter::Core::Execution::Windows < Facter::Core::Execution::Base

  def search_paths
    ENV['PATH'].split(File::PATH_SEPARATOR)
  end

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

  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 = %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i

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

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

  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

    if exe and (expanded = which(exe))
      expanded = "\"#{expanded}\"" if expanded.match(/\s+/)
      expanded << " #{args}" if args

      return expanded
    end
  end
end

Version data entries

121 entries across 121 versions & 2 rubygems

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