class Kitchen::Command::Base

Base class for CLI commands.

@author Fletcher Nichol <fnichol@nichol.ca>

Attributes

action[R]

@return [String] the action to perform @api private

args[R]

@return [Array] remainder of the arguments from processed ARGV @api private

config[R]

@return [Config] a Config object @api private

help[R]

@return [proc] a callable that displays help for the command @api private

options[R]

@return [Hash] hash of Thor options @api private

shell[R]

@return [Thor::Shell] a Thor shell object @api private

Public Class Methods

new(cmd_args, cmd_options, options = {}) click to toggle source

Contstructs a new Command object.

@param cmd_args [Array] remainder of the arguments from processed ARGV @param cmd_options [Hash] hash of Thor options @param options [Hash] configuration options @option options [String] :action action to take, usually corresponding

to the subcommand name (default: `nil`)

@option options [proc] :help a callable that displays help for the

command

@option options [Config] :config a Config object (default: `nil`) @option options [Loader] :loader a Loader object (default: `nil`) @option options [String] :shell a Thor shell object

# File lib/kitchen/command.rb, line 38
def initialize(cmd_args, cmd_options, options = {})
  @args = cmd_args
  @options = cmd_options
  @action = options.fetch(:action, nil)
  @help = options.fetch(:help, -> { "No help provided" })
  @config = options.fetch(:config, nil)
  @loader = options.fetch(:loader, nil)
  @shell = options.fetch(:shell)
end

Private Instance Methods

all_instances() click to toggle source

@return [Array<Instance>] an array of instances @raise [SystemExit] if no instances are returned @api private

# File lib/kitchen/command.rb, line 90
def all_instances
  result = @config.instances

  if result.empty?
    die "No instances defined"
  else
    result
  end
end
die(msg) click to toggle source

Emit an error message, display contextual help and then exit with a non-zero exit code.

Note This method calls exit and will not return.

@param msg [String] error message @api private

# File lib/kitchen/command.rb, line 81
def die(msg)
  error "\n#{msg}\n\n"
  help.call
  exit 1
end
filtered_instances(regexp) click to toggle source

Return an array on instances whos name matches the regular expression.

@param regexp [Regexp] a regular expression matching on instance names @return [Array<Instance>] an array of instances @raise [SystemExit] if no instances are returned or the regular

expression is invalid

@api private

# File lib/kitchen/command.rb, line 107
def filtered_instances(regexp)
  result = begin
    @config.instances.get(regexp) ||
      @config.instances.get_all(/#{regexp}/)
           rescue RegexpError => e
             die "Invalid Ruby regular expression, " \
               "you may need to single quote the argument. " \
               "Please try again or consult http://rubular.com/ (#{e.message})"
  end
  result = Array(result)

  if result.empty?
    die "No instances for regex `#{regexp}', try running `kitchen list'"
  else
    result
  end
end
logger() click to toggle source

@return [Logger] the common logger @api private

# File lib/kitchen/command.rb, line 127
def logger
  Kitchen.logger
end
parse_subcommand(arg = nil) click to toggle source

Return an array on instances whos name matches the regular expression, the full instance name, or the `“all”` literal.

@param arg [String] an instance name, a regular expression, the literal

`"all"`, or `nil`

@return [Array<Instance>] an array of instances @api private

# File lib/kitchen/command.rb, line 138
def parse_subcommand(arg = nil)
  arg == "all" ? all_instances : filtered_instances(arg)
end