Class: ShellTastic::IO

Inherits:
Object
  • Object
show all
Extended by:
Utils
Defined in:
lib/shelltastic/command_io.rb

Class Method Summary collapse

Methods included from Utils

string_nil_or_blank?

Class Method Details

.popen(command, timer) ⇒ Array

A wrapper around popen4

Parameters:

  • command (String)

    command(s) to execute

  • command (Array)

    command(s) to execute

  • command (Time)

    time object for run time

Returns:

  • (Array)

    array of hash meta-data for the command(s) executed

See Also:

  • #run


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/shelltastic/command_io.rb', line 16

def popen(command, timer)
  string_nil_or_blank?(command)
  command_results = {}
  begin
    start = timer.start
    return_code = Open4::popen4(command) do |pid, stdin, stdout, stderr|
                    command_results.store(:output,stdout.read.strip)
                    command_results.store(:pid,pid)
                    command_results.store(:error,stderr.read.strip)
                    stdin.close
                  end
    stop = timer.stop
    total_time = timer.total_time
    command_results.merge!(start: start, 
                           stop: stop,
                           command: command,
                           total_time: total_time, 
                           exitstatus: return_code.exitstatus)
  rescue Errno::ENOENT => e
    raise ShellTastic::CommandException.new("Shell command #{command} failed with status #{$?} and ERROR: #{e.message}")
  end
  command_results
end