Class: PdfTools::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf_tools/shell.rb

Overview

Send commands to the shell

Instance Method Summary collapse

Instance Method Details

#execute(command) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pdf_tools/shell.rb', line 20

def execute(command)
  stdout, stderr, status =
    Open3.popen3(*command) do |stdin, stdout, stderr, wait_thr|
      [stdin, stdout, stderr].each(&:binmode)
      stdout_reader = Thread.new { stdout.read }
      stderr_reader = Thread.new { stderr.read }

      [stdout_reader.value, stderr_reader.value, wait_thr.value]
    end

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

#run(command, **options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pdf_tools/shell.rb', line 8

def run(command, **options)
  stdout, stderr, status = execute(command)

  if status != 0
    raise PdfTools::Error, "Command `#{command.join(" ")}` failed!\n  Error: #{stderr}"
  end

  $stderr.print(stderr) unless options[:stderr] == false

  [stdout, stderr, status]
end