class Kitchen::Command::List

Command to list one or more instances.

@author Fletcher Nichol <fnichol@nichol.ca>

Public Instance Methods

call() click to toggle source

Invoke the command.

# File lib/kitchen/command/list.rb, line 28
def call
  result = parse_subcommand(args.first)
  if options[:debug]
    die "The --debug flag on the list subcommand is deprecated, " \
      "please use `kitchen diagnose'."
  elsif options[:bare]
    puts Array(result).map(&:name).join("\n")
  elsif options[:json]
    puts JSON.pretty_generate(Array(result).map { |r| to_hash(r) })
  else
    list_table(result)
  end
end

Private Instance Methods

color_pad(string) click to toggle source

Add a trailing ansi color escape code to line up columns of colored output.

@param string [String] a string @return [String] @api private

# File lib/kitchen/command/list.rb, line 50
def color_pad(string)
  string + colorize("", :white)
end
colorize(*args) click to toggle source

Colorize a string.

@return [String] a colorized string @api private

# File lib/kitchen/command/list.rb, line 143
def colorize(*args)
  shell.set_color(*args)
end
display_instance(instance) click to toggle source

Generate the display rows for an instance.

@param instance [Instance] an instance @return [Array<String>] @api private

# File lib/kitchen/command/list.rb, line 59
def display_instance(instance)
  [
    color_pad(instance.name),
    color_pad(instance.driver.name),
    color_pad(instance.provisioner.name),
    color_pad(instance.verifier.name),
    color_pad(instance.transport.name),
    format_last_action(instance.last_action),
    format_last_error(instance.last_error),
  ]
end
format_last_action(last_action) click to toggle source

Format and color the given last action.

@param last_action [String] the last action @return [String] formated last action @api private

# File lib/kitchen/command/list.rb, line 76
def format_last_action(last_action)
  case last_action
  when "create" then colorize("Created", :cyan)
  when "converge" then colorize("Converged", :magenta)
  when "setup" then colorize("Set Up", :blue)
  when "verify" then colorize("Verified", :yellow)
  when nil then colorize("<Not Created>", :red)
  else colorize("<Unknown>", :white)
  end
end
format_last_error(last_error) click to toggle source

Format and color the given last error.

@param last_error [String] the last error @return [String] formated last error @api private

# File lib/kitchen/command/list.rb, line 92
def format_last_error(last_error)
  case last_error
  when nil then colorize("<None>", :white)
  else colorize(last_error, :red)
  end
end
list_table(result) click to toggle source

Constructs a list display table and output it to the screen.

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

# File lib/kitchen/command/list.rb, line 103
def list_table(result)
  table = [
    [
      colorize("Instance", :green), colorize("Driver", :green),
      colorize("Provisioner", :green), colorize("Verifier", :green),
      colorize("Transport", :green), colorize("Last Action", :green),
      colorize("Last Error", :green)
    ],
  ]
  table += Array(result).map { |i| display_instance(i) }
  print_table(table)
end
print_table(*args) click to toggle source

Outputs a formatted display table.

@api private

to_hash(result) click to toggle source

Constructs a hashtable representation of a single instance.

@param result [Hash{Symbol => String}] hash of a single instance @api private

# File lib/kitchen/command/list.rb, line 120
def to_hash(result)
  {
    instance: result.name,
    driver: result.driver.name,
    provisioner: result.provisioner.name,
    verifier: result.verifier.name,
    transport: result.transport.name,
    last_action: result.last_action,
    last_error: result.last_error,
  }
end