Module: KBSecret::CLI::Command

Defined in:
lib/kbsecret/cli/command.rb,
lib/kbsecret/cli/command/cp.rb,
lib/kbsecret/cli/command/rm.rb,
lib/kbsecret/cli/command/env.rb,
lib/kbsecret/cli/command/new.rb,
lib/kbsecret/cli/command/conf.rb,
lib/kbsecret/cli/command/help.rb,
lib/kbsecret/cli/command/list.rb,
lib/kbsecret/cli/command/pass.rb,
lib/kbsecret/cli/command/todo.rb,
lib/kbsecret/cli/command/login.rb,
lib/kbsecret/cli/command/types.rb,
lib/kbsecret/cli/command/session.rb,
lib/kbsecret/cli/command/version.rb,
lib/kbsecret/cli/command/abstract.rb,
lib/kbsecret/cli/command/commands.rb,
lib/kbsecret/cli/command/raw_edit.rb,
lib/kbsecret/cli/command/sessions.rb,
lib/kbsecret/cli/command/generator.rb,
lib/kbsecret/cli/command/generators.rb,
lib/kbsecret/cli/command/stash_edit.rb,
lib/kbsecret/cli/command/stash_file.rb,
lib/kbsecret/cli/command/dump_fields.rb

Overview

The namespace for KBSecret's commands.

Defined Under Namespace

Classes: Abstract, Commands, Conf, Cp, DumpFields, Env, Generator, Generators, Help, List, Login, New, Pass, RawEdit, Rm, Session, Sessions, StashEdit, StashFile, Todo, Types, Version

Class Method Summary collapse

Class Method Details

.all_command_namesArray<String>

Returns the CLI-friendly names of all commands, internal and external.

Returns:

  • (Array<String>)

    the CLI-friendly names of all commands, internal and external



60
61
62
# File 'lib/kbsecret/cli/command.rb', line 60

def all_command_names
  internal_command_names + external_command_names
end

.external?(command_name) ⇒ Boolean

Returns whether or not there is an external command with the given name.

Returns:

  • (Boolean)

    whether or not there is an external command with the given name



29
30
31
# File 'lib/kbsecret/cli/command.rb', line 29

def external?(command_name)
  external_command_names.include?(command_name)
end

.external_command_namesObject

The CLI-friendly names of all external commands



22
23
24
25
26
# 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_pathsArray<String>

The fully-qualified paths of all external commands visible to KBSecret.

Returns:

  • (Array<String>)

    the fully-qualified paths of all external commands



15
16
17
18
19
# 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) ⇒ Boolean

Returns whether or not there is an internal command with the given name.

Returns:

  • (Boolean)

    whether or not there is an internal command with the given name



55
56
57
# File 'lib/kbsecret/cli/command.rb', line 55

def internal?(command_name)
  internal_command_names.include?(command_name)
end

.internal_command_classesArray<Class>

Returns the class objects of all non-abstract internal commands.

Returns:

  • (Array<Class>)

    the class objects of all non-abstract internal commands



34
35
36
37
38
# 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) ⇒ Class?

Returns the command class corresponding to the given name, or nil if the name does not correspond to an internal command.

Parameters:

  • command_name (String)

    the CLI-friendly name of the command

Returns:

  • (Class, nil)

    the command class corresponding to the given name, or nil if the name does not correspond to an internal command



48
49
50
51
52
# 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_namesArray<String>

Returns the CLI-friendly names of all internal commands.

Returns:

  • (Array<String>)

    the CLI-friendly names of all internal commands



41
42
43
# File 'lib/kbsecret/cli/command.rb', line 41

def internal_command_names
  internal_command_classes.map(&:command_name)
end

.run!(command_name, *args) ⇒ void

Note:

This method only takes internal command names.

Note:

This method may not return!

This method returns an undefined value.

Parameters:

  • command_name (String)

    the CLI-friendly name of the internal command to run

  • args (Array<String>)

    the arguments, if any, to pass to the command



69
70
71
72
73
74
75
76
77
# 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