module KBSecret::CLI::Command

The namespace for {KBSecret}'s commands.

Public Instance Methods

all_command_names() click to toggle source

@return [Array<String>] the CLI-friendly names of all commands, internal and external

# File lib/kbsecret/cli/command.rb, line 60
def all_command_names
  internal_command_names + external_command_names
end
external?(command_name) click to toggle source

@return [Boolean] whether or not there is an external command with the given name

# File lib/kbsecret/cli/command.rb, line 29
def external?(command_name)
  external_command_names.include?(command_name)
end
external_command_names() click to toggle source

The CLI-friendly names of all external commands

# File lib/kbsecret/cli/command.rb, line 22
def external_command_names
  external_command_paths.map do |c|
    File.basename(c, File.extname(c)).sub!("kbsecret-", "")
  end.freeze
end
external_command_paths() click to toggle source

The fully-qualified paths of all external commands visible to {KBSecret}. @return [Array<String>] the fully-qualified paths of all external commands

# File lib/kbsecret/cli/command.rb, line 15
def external_command_paths
  ENV["PATH"].split(File::PATH_SEPARATOR).map do |path|
    Dir[File.join(path, "kbsecret-*")]
  end.flatten.uniq.freeze
end
internal?(command_name) click to toggle source

@return [Boolean] whether or not there is an internal command with the given name

# File lib/kbsecret/cli/command.rb, line 55
def internal?(command_name)
  internal_command_names.include?(command_name)
end
internal_command_classes() click to toggle source

@return [Array<Class>] the class objects of all non-abstract internal commands

# File lib/kbsecret/cli/command.rb, line 34
def internal_command_classes
  klasses = constants.map(&Command.method(:const_get)).grep(Class)
  klasses.delete(Command::Abstract)
  klasses
end
internal_command_for(command_name) click to toggle source

@param command_name [String] the CLI-friendly name of the command @return [Class, nil] the command class corresponding to the given name, or `nil` if the name

does not correspond to an internal command
# File lib/kbsecret/cli/command.rb, line 48
def internal_command_for(command_name)
  klass = internal_command_classes.find { |c| c.command_name == command_name }
  # TODO: raise here if nil?
  klass
end
internal_command_names() click to toggle source

@return [Array<String>] the CLI-friendly names of all internal commands

# File lib/kbsecret/cli/command.rb, line 41
def internal_command_names
  internal_command_classes.map(&:command_name)
end
run!(command_name, *args) click to toggle source

@param command_name [String] the CLI-friendly name of the internal command to run @param args [Array<String>] the arguments, if any, to pass to the command @note This method only takes internal command names. @note This method may not return! @return [void]

# File lib/kbsecret/cli/command.rb, line 69
def run!(command_name, *args)
  klass = internal_command_for command_name
  cmd = klass.new(args)
  cmd.cli.guard do
    cmd.setup!
    cmd.validate!
    cmd.run!
  end
end