Sha256: ed9fd52c03126f4ea84f8f2d99377bcef49338000d6ab080181c89dc1e3627f3

Contents?: true

Size: 890 Bytes

Versions: 11

Compression:

Stored size: 890 Bytes

Contents

require 'open3'

module OmfCommon::Command
  # Execute a system command and use Open3 to capture exit status, stdout, stderr
  #
  # @example
  #
  #   OmfCommon::Command.execute("uname -a")
  def self.execute(*cmd, &block)
    result = nil
    begin
      Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thread|
        case wait_thread.value.exitstatus
        when 0
          # Exit status 0, all good, read stdout
          result = stdout.read.chomp
        when 1
          # Exit status 1, log minor error as warning
          logger.warn stderr.read.chomp
        when 2
          # Exit status 2, log standard error
          logger.error stderr.read.chomp
        else
          # Exit status greater than 2, log fatal error
          logger.fatal stderr.read.chomp
        end
      end
    rescue Errno::ENOENT => e
      logger.fatal e.message
    end
    result
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
omf_common-6.0.2.pre.2 lib/omf_common/command.rb
omf_common-6.0.2.pre.1 lib/omf_common/command.rb
omf_common-6.0.0 lib/omf_common/command.rb
omf_common-6.0.0.pre.11 lib/omf_common/command.rb
omf_common-6.0.0.pre.10 lib/omf_common/command.rb
omf_common-6.0.0.pre.8 lib/omf_common/command.rb
omf_common-6.0.0.pre.7 lib/omf_common/command.rb
omf_common-6.0.0.pre.6 lib/omf_common/command.rb
omf_common-6.0.0.pre.4 lib/omf_common/command.rb
omf_common-6.0.0.pre.3 lib/omf_common/command.rb
omf_common-6.0.0.pre.2 lib/omf_common/command.rb