Class: Brut::CLI::Runner

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

Overview

Runs a CLI app/command. Handles all the logic of parsing command line arguments, locating the command object to process the execution, allow for help requests, and understanding the output.

Instance Method Summary collapse

Constructor Details

#initialize(app_command, stdout:, stderr:, stdin:, project_root:) ⇒ Runner

Create the runner, which can be used to run app_command with whatever command line was provided.

Parameters:

  • app_command (Brut::CLI::Commands::BaseCommand)

    The app that is being executed, in which all command line arguments should be interpreted.

  • stdout (IO)

    the standard output, which will receive all output that isn't related to error messages.

  • stderr (IO)

    the standard error, which will receive error messages

  • stdin (IO)

    the standard input

  • project_root (Pathname)

    root of the Brut project (i.e. where Gemfile, app et. al. are located)



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/brut/cli/runner.rb', line 13

def initialize(app_command, stdout:, stderr:,stdin:, project_root:)
  prefix = if $0 =~ /\/brut$/
             "brut"
           else
             $0
           end
  @app_command  = app_command
  @stdout       = Brut::CLI::Output.new(io: stdout, prefix: "[ #{prefix} ] ")
  @stderr       = Brut::CLI::Output.new(io: stderr, prefix: "[ #{prefix} ] ")
  @stdin        = stdin
  @project_root = project_root
end

Instance Method Details

#run!(argv, env) ⇒ Integer

Run the commmand or subcommand based on the app_command given to the constructor and the command line provided in argv.

Parameters:

  • argv (Array)

    The arguments provided to the command. Outside of tests, this is almost certainly ARGV.

  • env (ENV|Hash)

    The UNIX environment available when the command was executed. This should be treated as if it's ENV, even though it may just be a hash.

Returns:

  • (Integer)

    the exit code to pass back to the shell that invoked the command, with 0 meaning the command succeeded and any other value meaning failure. Generally, this value should be between 0 and 255.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/brut/cli/runner.rb', line 34

def run!(argv, env)

  parsed_command_line = Brut::CLI::ParsedCommandLine.new(app_command: @app_command, argv:, env:)

  load_unix_environment!(env, parsed_command_line)
  setup_log_level(env, parsed_command_line)
  bootstrap!(env, parsed_command_line)

  execute_result = Brut::CLI::ExecuteResult.new do
    execution_context = Brut::CLI::Commands::ExecutionContext.new(
      argv: parsed_command_line.argv,
      options: Brut::CLI::Options.new(parsed_command_line.options),
      env:,
      stdout: @stdout,
      stderr: @stderr,
      stdin: @stdin
    )
    parsed_command_line.command.execute(execution_context)
  end
  execute_result.exit_status do |error_message|
    @stderr.puts error_message
  end
end