Module: R10K::Util::Commands

Defined in:
lib/r10k/util/commands.rb

Class Method Summary collapse

Class Method Details

.which(cmd) ⇒ String?

Find the full path of a shell command.

On POSIX platforms, the PATHEXT environment variable will be unset, so the first command named ‘cmd’ will be returned.

On Windows platforms, the PATHEXT environment variable will contain a semicolon delimited list of executable file extensions, so the first command with a matching path extension will be returned.

Parameters:

  • cmd (String)

    The name of the command to search for

Returns:

  • (String, nil)

    The path to the file if found, nil otherwise



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/r10k/util/commands.rb', line 17

def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir|
    exts.each do |ext|
      path = File.join(dir, "#{cmd}#{ext}")
      if File.executable?(path) && File.file?(path)
        return path
      end
    end
  end
  nil
end