Class: MotherBrain::Cli::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/mb/cli.rb

Overview

Note:

the arity of #initialize and #execute! are extremely important for testing purposes. It is a requirement to perform in-process testing with Aruba. In process testing is much faster than spawning a new Ruby process for each test.

This is the main entry point for the CLI. It exposes the method #execute! to start the CliGateway.

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel) ⇒ Runner

Returns a new instance of Runner.

Parameters:

  • argv (Array)
  • stdin (IO) (defaults to: STDIN)
  • stdout (IO) (defaults to: STDOUT)
  • stderr (IO) (defaults to: STDERR)
  • kernel (Kernel) (defaults to: Kernel)


19
20
21
# File 'lib/mb/cli.rb', line 19

def initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel)
  @argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel
end

Instance Method Details

#execute!Object

Start the CLI Gateway



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mb/cli.rb', line 24

def execute!
  MB::CliGateway.start(@argv)
rescue MBError => ex
  ui.error ex
  @kernel.exit(ex.exit_code)
rescue Ridley::Errors::ConnectionFailed => ex
  ui.error "[ERROR] Unable to connect to the configured Chef server: #{ex.message}."
  ui.error "[ERROR] Check your configuration and network settings and try again."
  @kernel.exit(MB::ChefConnectionError.exit_code)
rescue Thor::Error => ex
  ui.error ex.message
  @kernel.exit(1)
rescue Errno::EPIPE
  # This happens if a thor command is piped to something like `head`,
  # which closes the pipe when it's done reading. This will also
  # mean that if the pipe is closed, further unnecessary
  # computation will not occur.
  @kernel.exit(0)
end

#uiMB::Cli::Shell::Color, MB::Cli::Shell::Basic

Returns:

  • (MB::Cli::Shell::Color, MB::Cli::Shell::Basic)


45
46
47
# File 'lib/mb/cli.rb', line 45

def ui
  @ui ||= MB::Cli::Shell.shell.new
end