Module: CommandLine
- Defined in:
- lib/command_line.rb,
lib/command_line/result.rb,
lib/command_line/version.rb
Overview
CommandLine provides an easier way to run command-line applications. It captures all outputs, can handle applications that require stdin, and can pass environment variables. It's also helpful for testing commmand-line applications.
Defined Under Namespace
Classes: Error, ExitFailureError, Result, TimeoutError
Constant Summary collapse
- VERSION =
'2.0.1'
Class Method Summary collapse
-
.command_line(command, *args, env: {}, timeout: nil) {|stdin| ... } ⇒ Result
Run a command and get back the result.
-
.command_line!(*args, **kwargs, &block) ⇒ Result
Same as CommandLine.command_line except that a failure on exit raises an error.
Class Method Details
.command_line(command, *args, env: {}, timeout: nil) {|stdin| ... } ⇒ Result
Run a command and get back the result.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/command_line.rb', line 53 def command_line(command, *args, env: {}, timeout: nil) stdout = '' stderr = '' status = nil full_command = [command, *args].map(&:to_s).join(' ') environment = env.transform_keys(&:to_s) Open3.popen3(environment, full_command) do |i, o, e, wait_thr| threads = [] Timeout.timeout(timeout, TimeoutError) do yield i if block_given? threads << Thread.new { stdout = o.read } threads << Thread.new { stderr = e.read } threads.each(&:join) status = wait_thr.value end rescue TimeoutError => e threads.map(&:kill) raise e end Result.new(stdout, stderr, status) end |
.command_line!(*args, **kwargs, &block) ⇒ Result
Same as command_line except that a failure on exit raises an error.
95 96 97 98 99 |
# File 'lib/command_line.rb', line 95 def command_line!(*args, **kwargs, &block) command_line(*args, **kwargs, &block).tap do |result| raise ExitFailureError, result.stderr if result.failure? end end |