Module: PoiseLanguages::Utils::Which

Extended by:
Which
Included in:
PoiseLanguages::Utils, Which
Defined in:
lib/poise_languages/utils/which.rb

Overview

Replacement module for Chef::Mixin::Which with a slight improvement.

See Also:

Since:

  • 1.0.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.which(cmd, extra_path: %w{/bin /usr/bin /sbin /usr/sbin}, path: nil) ⇒ String, false

A replacement for Chef::Mixin::Which#which that allows using something other than an environment variable if needed.

Parameters:

  • cmd (String)

    Executable to search for.

  • extra_path (Array<String>) (defaults to: %w{/bin /usr/bin /sbin /usr/sbin})

    Extra directories to always search.

  • path (String, nil) (defaults to: nil)

    Replacement $PATH value.

Returns:

  • (String, false)

Since:

  • 1.0.0



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/poise_languages/utils/which.rb', line 34

def which(cmd, extra_path: %w{/bin /usr/bin /sbin /usr/sbin}, path: nil)
  # If it was already absolute, just return that.
  return cmd if cmd =~ /^(\/|([a-z]:)?\\)/i
  # Allow passing something other than the real env var.
  path ||= ENV['PATH']
  # Based on Chef::Mixin::Which#which
  # Copyright 2010-2017, Chef Softare, Inc.
  paths = path.split(File::PATH_SEPARATOR) + extra_path
  paths.each do |candidate_path|
    filename = ::File.join(candidate_path, cmd)
    return filename if ::File.executable?(filename)
  end
  false
end

Instance Method Details

#which(cmd, extra_path: %w{/bin /usr/bin /sbin /usr/sbin}, path: nil) ⇒ String, false

A replacement for Chef::Mixin::Which#which that allows using something other than an environment variable if needed.

Parameters:

  • cmd (String)

    Executable to search for.

  • extra_path (Array<String>) (defaults to: %w{/bin /usr/bin /sbin /usr/sbin})

    Extra directories to always search.

  • path (String, nil) (defaults to: nil)

    Replacement $PATH value.

Returns:

  • (String, false)

Since:

  • 1.0.0



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/poise_languages/utils/which.rb', line 34

def which(cmd, extra_path: %w{/bin /usr/bin /sbin /usr/sbin}, path: nil)
  # If it was already absolute, just return that.
  return cmd if cmd =~ /^(\/|([a-z]:)?\\)/i
  # Allow passing something other than the real env var.
  path ||= ENV['PATH']
  # Based on Chef::Mixin::Which#which
  # Copyright 2010-2017, Chef Softare, Inc.
  paths = path.split(File::PATH_SEPARATOR) + extra_path
  paths.each do |candidate_path|
    filename = ::File.join(candidate_path, cmd)
    return filename if ::File.executable?(filename)
  end
  false
end