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

empty_nil_blank?, string_nil_or_blank!, string_nil_or_blank?

Class Method Details

.popen(command, timer, formatter) ⇒ Hash

A wrapper around popen4

Examples:

{ :output, :pid, :error, :start, :stop, :total_time, :exitstatus }

Parameters:

  • command (String)

    command(s) to execute

  • command (Array)

    command(s) to execute

  • command (Time)

    time object for run time

  • command (Formatter)

    formatter object to build output

Returns:

  • (Hash)

    hash meta-data for the command executed

See Also:

  • #run


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

def popen(command, timer, formatter)
  string_nil_or_blank! command
  begin
    formatter.start = timer.start
    pid, stdin, stdout, stderr = Open4::popen4(command)
    stdin.close
    _, status = Process::waitpid2 pid
    error = stderr.read.strip
    formatter.build(command: command,
                    output: stdout.read.strip,
                    pid: pid,
                    error: string_nil_or_blank?(error) ? false : error,
                    stop: timer.stop,
                    exitstatus: status.exitstatus,
                    total_time: timer.total_time)

  rescue Errno::ENOENT => e
    raise ShellTastic::CommandException.new("Shell command #{command} failed with status #{$?} and ERROR: #{e.message}")
  end
  formatter.inspect
end