Sha256: ade50b4408e3fba14eba6182761e4eb24da65b5c94e14f9364c376868f0d511d

Contents?: true

Size: 974 Bytes

Versions: 3

Compression:

Stored size: 974 Bytes

Contents

require "mini_magick/logger"

require "open3"
require "timeout"

module MiniMagick
  ##
  # Sends commands to the shell (more precisely, it sends commands directly to
  # the operating system).
  #
  # @private
  #
  class Shell

    def initialize(whiny = true)
      @whiny = whiny
    end

    def run(command)
      stdout, stderr, code = execute(command)

      case code
      when 1
        fail MiniMagick::Error, "`#{command.join(" ")}` failed with error:\n#{stderr}"
      when 127
        fail MiniMagick::Error, stderr
      end if @whiny

      $stderr.print(stderr)

      stdout
    end

    def execute(command)
      stdout, stderr, status =
        MiniMagick.logger.debug(command.join(" ")) do
          Timeout.timeout(MiniMagick.timeout) do
            Open3.capture3(*command)
          end
        end

      [stdout, stderr, status.exitstatus]
    rescue Errno::ENOENT
      ["", "executable not found: \"#{command.first}\"", 127]
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mini_magick-4.0.2 lib/mini_magick/shell.rb
mini_magick-4.0.1 lib/mini_magick/shell.rb
mini_magick-4.0.0 lib/mini_magick/shell.rb