File: shell_capture.rb

package info (click to toggle)
ruby-cliver 0.3.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 208 kB
  • sloc: ruby: 760; makefile: 3
file content (35 lines) | stat: -rw-r--r-- 1,172 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require 'open3'

module Cliver
  class ShellCapture
    attr_reader :stdout, :stderr, :command_found

    # @overlaod initialize(command)
    #   @param command [String] the command to run
    # @overload initialize(command)
    #   @param command [Array<String>] the command to run; elements in
    #     the supplied array will be shelljoined.
    # @return [void]
    def initialize(command)
      command = command.shelljoin if command.kind_of?(Array)
      @stdout = @stderr = ''
      begin
        Open3.popen3(command) do |i, o, e|
          @stdout = o.read.chomp
          @stderr = e.read.chomp
        end
        # Fix for ruby 1.8.7 (and probably earlier):
        # Open3.popen3 does not raise anything there, but the error goes to STDERR.
        if @stderr =~ /open3.rb:\d+:in `exec': No such file or directory -.*\(Errno::ENOENT\)/ or
           @stderr =~ /An exception occurred in a forked block\W+No such file or directory.*\(Errno::ENOENT\)/
          @stderr = ''
          @command_found = false
        else
          @command_found = true
        end
      rescue Errno::ENOENT, IOError
        @command_found = false
      end
    end
  end
end