Module: CommandKit::Env::Path

Includes:
CommandKit::Env
Included in:
OpenApp, PackageManager, Pager
Defined in:
lib/command_kit/env/path.rb

Overview

Provides access to the PATH environment variable.

Environment Variables

  • PATH - The list of directories to search for commands within.

Instance Attribute Summary collapse

Attributes included from CommandKit::Env

#env

Instance Method Summary collapse

Instance Attribute Details

#path_dirsArray<String> (readonly)

The home directory.

Returns:

  • (Array<String>)


22
23
24
# File 'lib/command_kit/env/path.rb', line 22

def path_dirs
  @path_dirs
end

Instance Method Details

#command_installed?(name) ⇒ Boolean

Determines if the command is present on the system.

Examples:

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

Parameters:

  • name (String, Symbol)

    The command name.

Returns:

  • (Boolean)

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



81
82
83
# File 'lib/command_kit/env/path.rb', line 81

def command_installed?(name)
  !find_command(name).nil?
end

#find_command(name) ⇒ String?

Searches for the command in #path_dirs.

Parameters:

  • name (String, Symbol)

    The command name.

Returns:

  • (String, nil)

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



50
51
52
53
54
55
56
57
58
59
60
# 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

#initialize(**kwargs) ⇒ Object

Initializes #path_dirs using env['PATH'].

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.



32
33
34
35
36
# File 'lib/command_kit/env/path.rb', line 32

def initialize(**kwargs)
  super(**kwargs)

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