Sha256: 28b5d1d65a230389d5daac493258b49fb789c208a491bcdaaf3f2936d9e19037

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

# encoding: utf-8

require 'open4'

module ShellTastic
  class IO
    extend ShellTastic::Utils
    class << self
      # A wrapper around popen4
      #
      # @see #Command.run
      # @param command [String] command(s) to run
      # @return [Array, Hash] command_results the command results from shell
      # [{ :output, :pid, :error, :start, :stop, :total_time, :exitstatus }]
      def popen(command, timer=ShellTastic::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
    end
  end


end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shelltastic-0.2.5 lib/shelltastic/command_io.rb