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/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
Classes: CommandLineBuilder, CommandResult, CommandResultError, NoSuchFileError, NullLogger
Constant Summary collapse
- VERSION =
"1.3.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.
109 110 111 |
# File 'lib/awesome_spawn.rb', line 109 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.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/awesome_spawn.rb', line 63 def run(command, = {}) raise ArgumentError, "options cannot contain :out" if .include?(:out) raise ArgumentError, "options cannot contain :err" if .include?(:err) raise ArgumentError, "options cannot contain :in" if .include?(:in) = .dup params = .delete(:params) if (in_data = .delete(:in_data)) [:stdin_data] = in_data end output, error, status = "", "", nil command_line = build_command_line(command, params) output, error, status = launch(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.
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/awesome_spawn.rb', line 95 def run!(command, = {}) command_result = run(command, ) if command_result.failure? = "#{command} exit code: #{command_result.exit_status}" logger.error("AwesomeSpawn: #{}") logger.error("AwesomeSpawn: #{command_result.error}") raise CommandResultError.new(, command_result) end command_result end |