Module: AwesomeSpawn
- Extended by:
- AwesomeSpawn
- Included in:
- AwesomeSpawn
- Defined in:
- lib/awesome_spawn.rb,
lib/awesome_spawn/version.rb,
lib/awesome_spawn/null_logger.rb,
lib/awesome_spawn/spec_helper.rb,
lib/awesome_spawn/command_result.rb,
lib/awesome_spawn/no_such_file_error.rb,
lib/awesome_spawn/command_line_builder.rb,
lib/awesome_spawn/command_result_error.rb
Defined Under Namespace
Modules: SpecHelper Classes: CommandLineBuilder, CommandResult, CommandResultError, NoSuchFileError, NullLogger
Constant Summary collapse
- VERSION =
"1.5.0"
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#build_command_line(command, params = nil) ⇒ String
Build the full command line.
-
#run(command, options = {}) ⇒ CommandResult
Execute
command
synchronously via Kernel.spawn and gather the output stream, error stream, and exit status in a CommandResult. -
#run!(command, options = {}) ⇒ CommandResult
Same as #run, additionally raising a CommandResultError if the exit status is not 0.
Instance Attribute Details
#logger ⇒ Object
15 16 17 |
# File 'lib/awesome_spawn.rb', line 15 def logger @logger ||= NullLogger.new end |
Instance Method Details
#build_command_line(command, params = nil) ⇒ String
Build the full command line.
115 116 117 |
# File 'lib/awesome_spawn.rb', line 115 def build_command_line(command, params = nil) CommandLineBuilder.new.build(command, params) end |
#run(command, options = {}) ⇒ CommandResult
Execute command
synchronously via Kernel.spawn and gather the output
stream, error stream, and exit status in a CommandResult.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/awesome_spawn.rb', line 71 def run(command, = {}) bad_keys = (.keys.flatten & [:in, :out, :err]).map { |k| ":#{k}" } raise ArgumentError, "options cannot contain #{bad_keys.join(", ")}" if bad_keys.any? env, command_line, = (command, ) if (in_data = .delete(:in_data)) [:stdin_data] = in_data end output, error, status = launch(env, command_line, ) rescue Errno::ENOENT => err raise NoSuchFileError.new(err.) if NoSuchFileError.detected?(err.) raise else CommandResult.new(command_line, output, error, status) end |
#run!(command, options = {}) ⇒ CommandResult
Same as #run, additionally raising a CommandResultError if the exit status is not 0.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/awesome_spawn.rb', line 99 def run!(command, = {}) command_result = run(command, ) if command_result.failure? = CommandResultError.(command, command_result.exit_status) error = command_result.error.nil? || command_result.error.empty? ? command_result.output : command_result.error logger.error("AwesomeSpawn: #{}") logger.error("AwesomeSpawn: #{error}") raise CommandResultError.new(, command_result) end command_result end |