module CommandKit::Env::Path

Provides access to the `PATH` environment variable.

## Environment Variables

Attributes

path_dirs[R]

The home directory.

@return [String]

@api semipublic

Public Class Methods

new(**kwargs) click to toggle source

Initializes {#path_dirs} using `env`.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@api public

Calls superclass method CommandKit::Env::new
# File lib/command_kit/env/path.rb, line 32
def initialize(**kwargs)
  super(**kwargs)

  @path_dirs = env.fetch('PATH','').split(File::PATH_SEPARATOR)
end

Public Instance Methods

command_installed?(name) click to toggle source

Determines if the command is present on the system.

@param [String, Symbol] name

The command name.

@return [Boolean]

Specifies whether a command with the given name exists in one of the
{#path_dirs}.

@example

if command_installed?("docker")
  # ...
else
  abort "Docker is not installed. Aborting"
end

@api public

# File lib/command_kit/env/path.rb, line 81
def command_installed?(name)
  !find_command(name).nil?
end
find_command(name) click to toggle source

Searches for the command in {#path_dirs}.

@param [String, Symbol] name

The command name.

@return [String, nil]

The absolute path to the executable file, or `nil` if the command
could not be found in any of the {#path_dirs}.

@api public

# File lib/command_kit/env/path.rb, line 50
def find_command(name)
  name = name.to_s

  @path_dirs.each do |dir|
    file = File.join(dir,name)

    return file if File.file?(file) && File.executable?(file)
  end

  return nil
end