Sha256: 1c435043896c2ef071bec8946e84b58915c1f51d3c5f206dfe7d5858a4100c92

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# encoding: utf-8

require 'open4'

module ShellTastic
  class IO
    extend ShellTastic::Utils
    class << self
      # A wrapper around popen4
      #
      # @see #run
      # @param command [String] command(s) to execute
      # @param command [Array] command(s) to execute
      # @param command [Time] time object for run time
      # @param command [Formatter] formatter object to build output
      # @return [Hash] hash meta-data for the command executed
      # @example
      #   { :output, :pid, :error, :start, :stop, :total_time, :exitstatus }
      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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