Module: AwesomeSpawn
- Extended by:
- AwesomeSpawn
- Included in:
- AwesomeSpawn
- Defined in:
- lib/awesome_spawn.rb,
lib/awesome_spawn/version.rb,
lib/awesome_spawn/command_result.rb,
lib/awesome_spawn/no_such_file_error.rb,
lib/awesome_spawn/command_result_error.rb
Defined Under Namespace
Classes: CommandResult, CommandResultError, NoSuchFileError
Constant Summary collapse
- VERSION =
"1.1.1"
Instance Method Summary collapse
-
#build_command_line(command, params = nil) ⇒ String
Build the full command line.
-
#run(command, options = {}) ⇒ CommandResult
Execute
commandsynchronously 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 Method Details
#build_command_line(command, params = nil) ⇒ String
Build the full command line.
124 125 126 127 |
# File 'lib/awesome_spawn.rb', line 124 def build_command_line(command, params = nil) return command.to_s if params.nil? || params.empty? "#{command} #{assemble_params(sanitize(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.
56 57 58 59 60 61 62 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 56 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 :in_data is specified" if .include?(:in) && .include?(:in_data) = .dup params = .delete(:params) in_data = .delete(:in_data) output = "" error = "" status = nil command_line = build_command_line(command, params) begin output, error = launch(command_line, in_data, ) status = exitstatus ensure output ||= "" error ||= "" self.exitstatus = nil end 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 |
# File 'lib/awesome_spawn.rb', line 95 def run!(command, = {}) command_result = run(command, ) if command_result.exit_status != 0 = "#{command} exit code: #{command_result.exit_status}" raise CommandResultError.new(, command_result) end command_result end |