Class: Brut::CLI::Runner
- Inherits:
-
Object
- Object
- Brut::CLI::Runner
- 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
-
#initialize(app_command, stdout:, stderr:, stdin:, project_root:) ⇒ Runner
constructor
Create the runner, which can be used to run ‘app_command` with whatever command line was provided.
-
#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`.
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.
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:) @app_name = if $0 =~ /\/brut$/ "brut #{app_command.name}" else $0 end @app_command = app_command @stdout = stdout @stderr = stderr @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`.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/brut/cli/runner.rb', line 35 def run!(argv, env) parsed_command_line = Brut::CLI::ParsedCommandLine.new(app_command: @app_command, argv:, env:) logger = Brut::CLI::Logger.new( app_name: @app_command.name, stdout: @stdout, stderr: @stderr, theme: parsed_command_line.command.theme, ) 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.), env:, stdout: @stdout, stderr: @stderr, stdin: @stdin, logger: ) parsed_command_line.command.execute(execution_context) end execute_result.exit_status do || logger.fatal() @stderr.puts if parsed_command_line..log_file && !parsed_command_line..log_stdout? @stderr.puts @stderr.puts "More details may be available from the log file:" @stderr.puts @stderr.puts " " + parsed_command_line..log_file.to_s @stderr.puts @stderr.puts "You can also use --log-stdout to see these log messages in the terminal" end end end |